CLL

, by Prashant Gunjal


#include<iostream.h>
#include<conio.h>
#include<alloc.h>
#include<stdio.h>
struct list
{
int data;
struct list * next;

};
typedef struct list node;
node * head=NULL;
class sll
{
public:
node * getnode();
void create();
void display();
void deletef();
void deletel();
node * insertf();
void insertl();
void deletepos();
void insertpos();
};
node * sll::getnode()
{
node * temp;
temp = new node();
//or you can use//temp=(node *)malloc(sizeof(node));
cout<<"Enter Data";
cin>>temp->data;
temp->next=NULL;
return temp;
}
void sll ::create()
{
int no,i,x;
node * last,*temp;
cout<<"\nEnter no of nodes :";
cin>>no;
for(i=0;i<no;i++)
{
temp=getnode();
if(head==NULL)
{
head=temp;
head->next=head;
}
else
{
last=head;
while(last->next!=head)
{
last=last->next;
}
last->next=temp;
temp->next=head;
}

}
}
void sll::display()
{
node *p;
p=head;
cout<<"\nContent\n";
do
{
cout<<p->data<<" ";
p=p->next;

}while(p!=head);

}


node * sll::insertf()
{
node * p=NULL,*last=NULL;
int x;
p= getnode();
last=head;
while(last->next!=head)
{
last=last->next;
}
p->next=head;
head=p;
last->next=head;
return head;
}
void sll::insertl()
{
node * p=NULL,*temp=NULL;
p=getnode();
temp=head;
while(temp->next!=head)
{
temp=temp->next;
}
temp->next=p;
p->next=head;
}

void sll::insertpos()
{
node * temp=NULL,*last;
int pos,i;
cout<<"Enter position no. aeter to which to insert ";
cin>>pos;
if(pos>1)
{
i=1;
last=head;
while(last->next!=head&&i<pos)
{
last=last->next;
i++;
}
if(last!=NULL)
{
temp=getnode();
temp->next=last->next;
last->next=temp;
}
}
else
cout<<"Not possible" ;
}

void sll ::deletef()
{
node * p,* last=NULL;
p=head;
last=head;
while(last->next!=head)
{
last=last->next;
}
head=head->next;
last->next=head;
delete (p);
}
void sll:: deletel()
{
node * p=NULL;
p=head;
while(p->next->next!=head)
{
p=p->next;

}
delete (p->next);
p->next=head;

}

void sll::deletepos()
{
node * temp=NULL,*last;
int pos,i;
cout<<"Enter position no. to delete node ";
cin>>pos;
if(pos>1)
{
i=1;
last=head;
while(last->next!=head&&i<pos-1)
{
last=last->next;
i++;
}
if(last!=head)
{
temp=last->next;
last->next=temp->next;
}
}
else
cout<<"Not possible" ;
}
void main()
{
int ch,i;
sll l;
clrscr();
do
{
cout<<"\n1.Create\n2.Display\n3.Insert First";
cout<<"\n4.Insert Last\n5.Delete First\n6.Delete Last";
cout<<"\n7.Insert by pos after\n8.delete by pos\n9.Exit";
cout<<"\nEnter your choice\t";
cin>>ch;

switch(ch)
{
case 1:
l.create();
break;

case 2:
l.display();
break;
case 3:
head=l.insertf();
break;
case 4:
l.insertl();
case 5:
l.deletef();
break;
case 6:
l.deletel();
break;
case 7:
l.insertpos();
break;
case 8:
l.deletepos();
break;
}
}while(ch<9);
       getch();

}

/* OUTPUT
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       1

Enter no of nodes :5
Enter Data2
Enter Data3
Enter Data4
Enter Data5
Enter Data6

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
2 3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       3
Enter Data1

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
1 2 3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       4
Enter Data56

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       5

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 5 6 56
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       6

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice 7
Enter position no. aeter to which to insert 2
Enter Data34

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2


Content
3 4 34 5 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       8
Enter position no. to delete node 4

1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       2

Content
3 4 34 6
1.Create
2.Display
3.Insert First
4.Insert Last
5.Delete First
6.Delete Last
7.Insert by pos after
8.delete by pos
9.Exit
Enter your choice       9
*/

0 comments: