Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- struct list{
- int data;
- struct list *next;
- };
- typedef struct list node;
- node *head,*tail,*prev,*last,*temp;
- void showMenu()
- {
- printf("\n1.Insert\n2.Delete\n3.Exit\n");
- }
- node *insertion(node *head,node *tail)
- {
- node *temp;
- int item;
- printf("ISERT ITEM: ");
- scanf("%d",&item);
- temp=new node();
- temp->data=item;
- if(head->data>temp->data)
- {
- temp->next=head;
- head=temp;
- return head;
- }
- else if(temp->data>tail->data)
- {
- node *x;
- x=head;
- while(x->next!=NULL)
- {
- x=x->next;
- }
- x->next=temp;
- x=temp;
- temp->next=NULL;
- return head;
- }
- else if(head->data<item)
- {
- node *x;
- x=head;
- while(x->next->data < item)
- {
- x=x->next;
- }
- temp->next=x->next;
- x->next=temp;
- return head;
- }
- return head;
- }
- node *deletation(node *head,node *tail)
- {
- node *temp,*prev;
- int item;
- printf("DELETE ITEM: ");
- scanf("%d",&item);
- if(head->data==item)
- {
- temp=head;
- head=head->next;
- delete(temp);
- return head;
- }
- else
- {
- temp=head;
- while(temp->data != item)
- {
- prev=temp;
- temp=temp->next;
- }
- prev->next=temp->next;
- delete(temp);
- return head;
- }
- return head;
- }
- void display(node *head)
- {
- node *temp;
- int len=0;
- temp=head;
- while(temp!=NULL)
- {
- printf("%d ",temp->data);
- temp=temp->next;
- len++;
- }
- printf("LENGTH IS: %d\n",len);
- printf("\n");
- }
- int main()
- {
- int ch,i=1;
- head=NULL;
- printf("DO YOU WANT TO ADD ITEM: ");
- scanf("%d",&ch);
- while(ch==1)
- {
- if(head==NULL)
- {
- head=new node();
- printf("ENTER DATA: ");
- scanf("%d",&head->data);
- head->next=NULL;
- tail=head;
- }
- else{
- temp=new node();
- printf("ENTER DATA: ");
- scanf("%d",&temp->data);
- temp->next=NULL;
- tail->next=temp;
- tail=temp;
- }
- printf("DO YOU WANT TO ADD ITEM: ");
- scanf("%d",&ch);
- }
- display(head);
- do{
- showMenu();
- int x;
- scanf("%d",&x);
- if(x==1)
- {
- head=insertion(head,tail);
- display(head);
- }
- else if(x==2)
- {
- head=deletation(head,tail);
- display(head);
- }
- else{
- i=0;
- }
- }while(i=1);
- }
Advertisement
Add Comment
Please, Sign In to add comment