Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include<stdlib.h>
- struct NODE
- {
- int data;
- struct NODE *next;
- };
- typedef struct NODE node;
- node *insertion(node *start,int newitem);
- node *deletion(node *start,int item);
- void display (node *start);
- int main()
- {
- node *start,*prev,*temp,*temp1;
- int ans,j=0,item,loop;
- start = NULL;
- while(j<5)
- {
- printf("Enter 1 for creation:\nEnter 2 for display:\nEnter 3 for deletion:\nEnter 4 for insertion:\nExit:\n");
- scanf("%d",&j);
- printf("\n");
- if(j==1)
- {
- do
- {
- printf("press 1 to create and 0 to stop\n");
- scanf("%d", &ans);
- if(ans == 1)
- {
- if(start == NULL)
- {
- start =(node*)malloc(sizeof(node));
- start->next = NULL;
- printf("Enter data for node: ");
- scanf("%d", &start->data);
- prev = start;
- }
- else
- {
- temp = (node*)malloc(sizeof(node));
- temp->next = NULL;
- prev->next = temp;
- printf("Enter data for Node: ");
- scanf("%d", &temp->data);
- prev=temp;
- }
- }
- }
- while(ans == 1);
- }
- if(j==2)
- display(start);
- if(j==3)
- {
- while(1)
- {
- node *deletion(node *start,int item);
- while(1)
- {
- printf("\n Enter the item to be deleted:=");
- scanf("%d",&item);
- start=deletion(start,item);
- printf("\n");
- display(start);
- printf("\n");
- printf("Do you want to return to previous function: (1=yes 2=no)");
- scanf("%d",&loop);
- if(loop==1)
- break;
- }
- if(loop==1)
- break;
- }
- }
- if(j==4)
- {
- while(1)
- {
- printf("\n Enter a item to be inserted:=");
- scanf("%d",&item);
- start=insertion(start,item);
- display(start);
- printf("\n");
- printf("Do you want to return to previous function: (1=yes 2=no)");
- scanf("%d",&loop);
- if(loop==1)
- break;
- }
- }
- }
- return 0;
- }
- node *deletion(node *start, int item)
- {
- node *temp1,*temp;
- if(start==NULL)
- {
- printf("\nNo linked list!");
- }
- else if (start->data==item)
- {
- temp1=start;
- start=start->next;
- free(temp1);
- }
- else
- {
- temp=start;
- while((temp->next!=NULL)&&(temp->next->data!=item))
- {
- temp=temp->next;
- }
- if(temp->next==NULL)
- {
- printf("\n Not Found");
- }
- else
- {
- temp1=temp->next;
- temp->next=temp1->next;
- free(temp1);
- }
- }
- return start;
- }
- node *insertion(node *start, int newitem)
- {
- node *temp,*temp1;
- if((start==NULL)||(start->data>=newitem))
- {
- temp1=(node*)malloc(sizeof(node));
- temp1->data=newitem;
- temp1->next=start;
- start=temp1;
- }
- else
- {
- temp=start;
- while((temp->next!=NULL)&&(temp->next->data<=newitem))
- {
- temp=temp->next;
- }
- temp1=(node*)malloc(sizeof(node));
- temp1->data=newitem;
- temp1->next=temp->next;
- temp->next=temp1;
- }
- return start;
- }
- void display(node *head)
- {
- node *temp;
- printf("\n Created list:=\n");
- temp=head;
- while(temp!=NULL)
- {
- printf(" %d",temp->data);
- temp=temp->next;
- }
- printf("\n");
- return;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement