Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- struct node
- {
- int value;
- struct node *next;
- };
- struct node *head = NULL,*tail = NULL;
- void insertlast(int x);
- void print();
- void remove(int x);
- int length();
- int main()
- {
- while(1)
- {
- printf("...MENU...\n");
- printf("(1) Insert Last\n");
- printf("(2) Show All\n");
- printf("(3) Remove\n");
- printf("(4) Exit\n");
- printf("------------\n");
- printf("Enter Your Choice:");
- int choice;
- scanf("%d",&choice);
- int x;
- switch(choice)
- {
- case 1:
- printf("Enter Number: ");
- scanf("%d",&x);
- insertlast(x);
- print();
- break;
- case 2:
- print();
- break;
- case 3:
- printf("Which value you want to remove : ");
- scanf("%d",&x);
- remove(x);
- print();
- break;
- case 4:
- exit(0);
- }
- getchar();
- }
- }
- void print()
- {
- node *currentnode=head;
- printf("\nYour List is: ");
- while(currentnode!=NULL)
- {
- printf("%d ",currentnode->value);
- currentnode= currentnode->next;
- }
- printf("\n\n\n");
- }
- void insertlast(int x)
- {
- node *temp=(node*)malloc(sizeof(node));
- if(head==NULL)
- {
- temp->value=x;
- temp->next=NULL;
- head=temp;
- tail=temp;
- }
- else
- {
- temp->value=x;
- temp->next=NULL;
- tail->next=temp;
- tail=temp;
- }
- }
- int search(int x)
- {
- node *currentnode=head;
- while(currentnode!=NULL)
- {
- if(currentnode->value==x)
- return 1;
- currentnode= currentnode->next;
- }
- return 0;
- }
- int length()
- {
- int l=0;
- node *currentnode=head;
- while(currentnode!=NULL)
- {
- l++;
- currentnode= currentnode->next;
- }
- return l;
- }
- void remove(int x)
- {
- node *temp=NULL,*prev=NULL,*currentnode =head;
- if(search(x))
- {
- if(head->value==x)
- {
- temp=head;
- head=head->next;
- free(temp);
- }
- else
- {
- while(currentnode->next!=NULL)
- {
- if(currentnode->next->value==x)
- {
- prev=currentnode;
- temp=currentnode->next;
- break;
- }
- else
- currentnode=currentnode->next;
- }
- if(temp->next==NULL)
- {
- prev->next=NULL;
- tail=prev;
- free(temp);
- }
- else
- {
- prev->next=temp->next;
- free(temp);
- }
- }
- }
- else
- printf("Not found for remove");
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement