Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- #include<conio.h>
- #include<stdlib.h>
- #define null 0
- using namespace std;
- struct node
- {
- int data;
- struct node *ptr;
- };
- typedef struct node node;
- class llist
- {
- node *head;
- public:void create(void)
- {
- head=null;
- }
- void insert_beg(int data);
- void insert_pos(int pos,int data);
- void insert_end(int data);
- void display(void);
- };
- void llist::insert_beg(int data)
- {
- node *temp=new node;
- temp->data=data;
- temp->ptr=head;
- head=temp;
- }
- void llist::insert_pos(int pos,int data)
- {
- node *temp=new node;
- temp->data=data;
- node *p=head;
- int i=0;
- while(i!=pos-1)
- {
- p=p->ptr;
- i++;
- }
- temp->ptr=p->ptr;
- p->ptr=temp;
- }
- void llist::insert_end(int data)
- {
- node *temp=new node;
- temp->data=data;
- node *p=head;
- while(p->ptr!=null)
- {
- p=p->ptr;
- }
- p->ptr=temp;
- temp->ptr=NULL;
- }
- void llist::display()
- {
- node *temp=head;
- while(temp!=null)
- {
- cout<<"\nElement:"<<temp->data;
- temp=temp->ptr;
- }
- }
- int main()
- {
- int choice,pos,data;
- char ch;
- llist l1;
- l1.create();
- do
- {
- cout<<"\nMAIN MENU";
- cout<<"\n1.First\n2.Inbetween\n3.Last\n4.Display";
- cout<<"\nEnter your choice of insertion:";
- cin>>choice;
- switch(choice)
- {
- case 1:cout<<"\Enter the data:";
- cin>>data;
- l1.insert_beg(data);
- break;
- case 2:cout<<"\nEnter the data:";
- cin>>data;
- cout<<"\nEnter the position:";
- cin>>pos;
- l1.insert_pos(pos,data);
- break;
- case 3:cout<<"\nEnter the data:";
- cin>>data;
- l1.insert_end(data);
- break;
- case 4:l1.display();
- break;
- case 5:cout<<"\nInvalid choice";
- }
- cout<<"\nDo you wish to continue(y/n):";
- cin>>ch;
- fflush(stdin);
- }while(ch=='y');
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement