You are on page 1of 4

/* SINGLE LINKED LIST WITHOUT TEMPLATES */

#include<iostream.h>
#include<conio.h>
#include<stdlib.h>

class node
{
public:
int data;
node *next;
};
class SLL
{
node *head;
public:
SLL()
{
head=NULL;
}
void Insert_beg();
void Insert_mid();
void Insert_end();
void Delete_beg();
void Delete_mid();
void Delete_end();
void Display();
};

void SLL :: Display()


{
node *p=head;
while(p!=NULL)
{
cout<<p->data<<"->";
p=p->next;
}

}
void SLL :: Insert_beg()
{
node *p,*temp=head;
P=new Node;
cout<<"Enter Data For The New Node : ";
cin>>x;
p->data=x;
p->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
p->Next=head;
head=p;
}
}

void SLL :: Insert_mid()


{
node *p,*temp=head;
int i,pos;
P=new node;
cout<<"Enter Data For The New Node : ";
cin>>x;
cout<<"Enter Position To Insert : ";
cin>>i;
P->data=x;
P->next=NULL;
if(head==NULL)
{
head=p;
}
else
{
temp=head;
while(i=0;i<pos-1&&temp!=NULL;i++)
{
temp=temp->next;
}
p->next=temp->next;
temp->next=p;
}

void SLL :: Insert_end()


{
node *p,*temp=head;
int i,pos;
p=new node;
cout<<"Enter Data For The New Node : ";
cin>>x;
p->data=x;
p->next=NULL;
if(head==NULL)
{
head=p;
}
temp=head;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=p;
}

void SLL :: Delete_beg()


{
node *p;
if(head==NULL)
cout<<"\n deletion not possible";
else
{
p=head;
head=p->next;
}
delete p;
}

void SLL :: Delete_mid()


{
node *p ,*temp=head;
int pos;
cout<<"Enter Position To Delete : ";
cin>>pos;
if(head==NULL)
cout<<"\n deletion not possible";
else
{
for(i=0; i<pos-1 && temp!= NULL;i++)
{
temp=temp->next;
}
p=temp->next;
temp->next=p->next;
}
delete p;
}

void SLL :: Delete_end()


{
node *p ,*temp=head;
int pos;
cout<<"Enter Position To Delete : ";
cin>>pos;
if(head==NULL)
cout<<"\n deletion not possible";
else
{
while(temp->next->next!=NULL)
{
temp=temp->next;
}
p=temp->next;
temp->next=NULL;
}
delete p;
}

int main()
{
SLL L;
int ch;
clrscr();
while(1)
{
cout<<"CHOICE OPERATION\n";
cout<<" 1 Insert at begining \n";
cout<<" 2 Insert at end \n";
cout<<" 3 Insert in middle \n";
cout<<" 4 deltion from begining \n";
cout<<" 5 deltion from end \n";
cout<<" 6 deltion from middle \n";
cout<<" 7 Exit\n";
cout<<"Enter Your Choice : ";
cin>>ch;
switch(ch)
{
case 1: L.Insert_beg();
break;
case 2: L.Insert_end();
break;
case 3: L.Insert_mid();
break;
case 4: L.Delete_beg();
break;
case 5: L.Delete_end();
break;
case 6: L.Delete_mid();
break;
case 7: exit(0);
default: cout<<"Wrong Choice\n";
}
}
return 0;
}
}

You might also like