Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<stdlib.h>
- struct node
- {
- int val;
- struct node *next;
- };
- struct node *head=NULL,*last=NULL;
- void insert(int value)
- {
- struct node *tmp;
- tmp=(struct node *)malloc(sizeof(node));
- tmp->val=value;
- tmp->next=NULL;
- ///for the first element in the list
- if(head==NULL)
- {
- head=tmp;
- last=tmp;
- }
- else
- {
- last->next=tmp;
- last=tmp;
- }
- }
- void printlist()
- {
- struct node *tmp=head;
- while(tmp!=NULL)
- {
- printf("The list Value:%d\n\n",tmp->val);
- tmp=tmp->next;
- }
- }
- int search(int value)
- {
- struct node* tmp=head;
- while(tmp!=NULL)
- {
- if(tmp->val==value)
- return 1;
- tmp=tmp->next;
- }
- return 0;
- }
- void deletenode(int value)
- {
- struct node* tmp=head;
- struct node* prev=NULL;
- while(tmp!=NULL)
- {
- if(tmp->val==value)
- {
- if(prev==NULL)
- {
- head=tmp->next;
- printf("The number is delete\n\n");
- }
- else{
- prev->next=tmp->next;
- printf("The number is delete\n\n");
- }
- break;
- }
- prev=tmp;
- tmp=tmp->next;
- }
- }
- void menu () {
- while (1) {
- printf("Press 1 to insert.\n");
- printf("Press 2 to search.\n");
- printf("Press 3 to delete node.\n");
- printf("Press 4 to print list.\n");
- printf("Press 0 to Exit.\n");
- printf("\n");
- int n;
- scanf("%d", &n);
- if (n==0) break;
- else if (n==1) {
- printf("Enter the number: ");
- int num;
- scanf("%d", &num);
- printf("\n");
- insert(num);
- }
- else if (n==2) {
- printf("Enter the number: ");
- int num;
- scanf("%d", &num);
- printf("\n");
- if (search(num)) printf("The Number is found.\n\n");
- else printf("The Number is Not found.\n\n");
- }
- else if (n==3) {
- printf("Enter the number: ");
- int num;
- scanf("%d", &num);
- printf("\n");
- deletenode(num) ;
- }
- else if (n==4) printlist();
- else {
- printf("Invalid choice! Try again.\n\n");
- }
- }
- }
- int main () {
- menu();
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement