Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<stdio.h>
- #include<iostream>
- #include<malloc.h>
- #include<string.h>
- using namespace std;
- void display(void);
- void insert_first(void);
- void insert_last(void);
- void insert_loc(void);
- void delete_specific(void);
- void delete_first(void);
- void delete_last(void);
- char item[10];
- struct node{
- char info[10];
- struct node* link;
- }*start=NULL;
- int main()
- {
- int choice;
- do{
- printf("\n\n 1)insert first\n 2)insert last\n 3)insert loc\n 4)display\n 5)delete item\n 6)delete first\n 7)delete last\n 8)exit\n");
- printf("enter choice:");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- insert_first();
- break;
- case 2:
- insert_last();
- break;
- case 3:
- insert_loc();
- break;
- case 4:
- display();
- break;
- case 5:
- delete_specific();
- break;
- case 6:
- delete_first();
- break;
- case 7:
- delete_last();
- break;
- case 8:
- printf("\nBYE BYE!!!!");
- return 0;
- default :
- printf("invalid key...please try again!!!");
- }
- }while(1);
- }
- void insert_first(void)
- {
- int n=1;
- char item1[10];
- struct node *ptr;
- printf("item to be inserted:");
- cin>>item1;
- if(start==NULL)
- {
- start=(struct node *)malloc(sizeof(struct node));
- strcpy(start->info,item1);
- start->link=NULL;
- }
- else
- {
- ptr=start;
- start=(struct node *)malloc(sizeof(struct node));
- strcpy(start->info,item1);
- start->link=ptr;
- }
- cout<<"item inserted:"<<item1;
- }
- void insert_last(void)
- {
- struct node *ptr;
- printf("item to be inserted:");
- if(start==NULL)
- {
- start=(struct node *)malloc(sizeof(struct node));
- cin>>start->info;
- start->link=NULL;
- }
- else
- {
- ptr=start;
- while(ptr->link!=NULL)
- ptr=ptr->link;
- ptr->link=(struct node *)malloc(sizeof(struct node));
- ptr=ptr->link;
- cin>>ptr->info;
- ptr->link=NULL;
- }
- printf("item inserted:%s",ptr->info);
- }
- void insert_loc(void)
- {
- struct node *ptr,*newn;
- ptr=start;
- newn=start;
- if(start==NULL)
- cout<<"\nthe list is empty!!!,insert atleast one node...\n";
- else
- {
- cout<<"enter an item after which a node to be inserted:";
- cin>>item;
- cout<<"\nitem to be inserted:";
- while(ptr!=NULL)
- {
- if(strcmp(ptr->info,item)==0)
- {
- newn=(struct node *)malloc(sizeof(struct node));
- cin>>newn->info;
- newn->link=ptr->link;
- ptr->link=newn;
- break;
- }
- else
- ptr=ptr->link;
- }
- }
- cout<<"item inserted:"<<newn->info;
- return;
- }
- void display(void)
- {
- struct node *ptr=start;
- int n=1;
- if(ptr!=NULL)
- {
- while(ptr!=NULL)
- {
- printf("no %d:%s\n",n++,ptr->info);
- ptr=ptr->link;
- }
- }
- else
- {
- printf("List is empty!!!\n");
- }
- }
- void delete_specific(void)
- {
- struct node *ptr, *prev;
- printf("\n\nEnter ITEM which is to be deleted: ");
- scanf("%s", &item);
- if (start == NULL)
- printf("\n\nLinked list is empty!!!.\n");
- else if (strcmp(start->info,item)==0)
- {
- ptr = start;
- start = start->link;
- free(ptr);
- }
- else
- {
- ptr = start;
- prev = start;
- while (ptr != NULL)
- {
- if (strcmp(ptr->info,item)==0)
- {
- prev->link = ptr->link;
- free(ptr);
- printf("\n\nItem deleted: %s", item);
- break;
- }
- else
- {
- prev = ptr;
- ptr = ptr->link;
- }
- }
- printf("\n\nItem deleted: %s", item);
- }
- }
- void delete_first(void)
- {
- struct node *ptr;
- ptr=start;
- if(start==NULL)
- printf("the list is empty.\n");
- else
- {
- strcpy(item,start->info);
- start=start->link;
- }
- printf("item deleted:%s",item);
- free(ptr);
- }
- void delete_last()
- {
- struct node *ptr, *prev;
- if (start == NULL)
- printf("\n\nLinked list is empty.\n");
- else
- {
- ptr = start;
- prev = start;
- while (ptr->link != NULL)
- {
- prev = ptr;
- ptr = ptr->link;
- }
- strcpy(item,ptr->info);
- if (start->link == NULL)
- start = NULL;
- else
- prev->link = NULL;
- free(ptr);
- printf("\n\nItem deleted: %s", item);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment