Advertisement
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_loc(void);
- void insert_first(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 3)insert loc\n 4)display\n 8)exit\n");
- printf("enter choice:");
- scanf("%d",&choice);
- switch(choice)
- {
- case 1:
- insert_first();
- break;
- case 3:
- insert_loc();
- break;
- case 4:
- display();
- break;
- case 8:
- printf("\nBYE BYE!!!!");
- return 0;
- default :
- printf("invalid key...please try again!!!");
- }
- }while(1);
- }
- 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 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;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement