/*--------------------------------------------------------------------------------------------
Program : Write a menu driven program to perform
following
operations on SLL/CDLL :
1. Create
2. Insert -
Start, end,between
3. Search
4. Delete - Start, end,between
5. Reverse
6. Display
Created
By : JABIR DAUD PATHAN
Branch : COMPUTER ENGINEERING
---------------------------------------------------------------------------------------------*/
/*--------------------<< Header File
>>---------------------*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
/*------------<<
Global Declaration of Functions
>>-----------*/
void
create(void);
void
display(void);
void
insert_beginning(void);
void
insert_between(void);
void
insert_end(void);
void
delete_beginning(void);
void
delete_between(void);
void
delete_end(void);
void
search(void);
void
reverse(void);
typedef
struct node
{
int data;
struct node*next;
}node;
node*head,*nwnode;
int
ch,no,i,j; //Global Declaration of
variables
/*-------<<
function defination for create linked list >>------*/
void
create()
{
printf("\nEnter no. of
nodes==>");
scanf("%d",&no);
printf("\n\nEnter values of
node==>");
head=(node*)malloc(sizeof(node));
scanf("%d",&(head->data));
head->next=NULL;
nwnode=head;
for(i=1;i<no;i++)
{
nwnode->next=(node*)malloc(sizeof(node));
nwnode=nwnode->next;
printf("\nEnter values of
node==>");
scanf("%d",&(nwnode->data));
nwnode->next=NULL;
}
}
/*-------<<
function defination for display linked list >>------*/
void
display()
{
node*p;
printf("\n\nCreated link
list==>\n\n");
printf("head--->");
for(p=head;p!=NULL;p=p->next)
{
printf("%d--->",(p->data));
}
printf("NULL");
}
/*-------<<
function defination for insert_beginning >>------*/
void
insert_beginning()
{
node *nwnode;
printf("\nEnter the value to be
inserted==>");
scanf("%d",&j);
nwnode=(node*)malloc(sizeof(node));
nwnode->data=j;
nwnode->next=head;
head=nwnode;
}
/*-------<<
function defination for insert_between >>------*/
void
insert_between()
{
node *nwnode,*prev;
int m,n;
printf("\nEnter the value to be
inserted==>");
scanf("%d",&m);
nwnode=(node*)malloc(sizeof(node));
nwnode->data=m;
nwnode->next=NULL;
printf("\n\nEnter the previous
node==>");
scanf("%d",&n);
prev=head;
while(prev!=NULL && prev->data!=n)
{
prev=prev->next;
}
if(prev!=NULL)
{
nwnode->next=prev->next;
prev->next=nwnode;
}
else
printf("\nData not found");
}
/*-------<<
function defination for insert_end >>------*/
void
insert_end(void)
{
node *nwnode,*p;
printf("\nEnter the value to be
inserted==>");
scanf("%d",&j);
nwnode=(node*)malloc(sizeof(node));
nwnode->data=j;
nwnode->next=NULL;
p=head;
while(p->next!=NULL)
{
p=p->next;
}
p->next=nwnode;
}
/*-------<<
function defination for delete_beginning >>------*/
void
delete_beginning()
{
node *p;
if(head==NULL)
{
printf("\n\n\tUnderflow.......!");
}
p=head;
head=head->next;
free(p);
}
/*-------<<
function defination for delete_between >>------*/
void
delete_between()
{
node *p,*q;
int x,i;
printf("\nEnter the data to be deleted :
");
scanf("%d",&x);
if(head==NULL)
{
printf("\n\n\tUnderflow.......!");
}
if(head->data==x)
{
p=head;
head=head->next;
free(p);
}
q=head;
while(q->next->data!=x &&
q->next !=NULL)
{
q=q->next;
}
p=q->next;
q->next=q->next->next;
free(p);
}
/*-------<<
function defination for delete_end >>------*/
void
delete_end()
{
node *p,*q;
if(head==NULL)
{
printf("\n\n\tUnderflow.......!");
}
p=head;
if(head->next==NULL)
{
head=NULL;
free(p);
}
q=head;
while(q->next->next!=NULL)
{
q=q->next;
}
p=q->next;
q->next=NULL;
free(p);
}
/*-------<<
function defination for search >>------*/
void
search()
{
node *p;
int m,cnt=1;
printf("\n\nEnter the data to be
searched: ");
scanf("%d",&m);
p=head;
while(p!=NULL && p->data !=m)
{
cnt++;
p=p->next;
}
if(p==NULL)
printf("\nNot found");
else
printf("\n\tFound at
location=%d",cnt);
printf("\n\n\tSearched link is
%d->%d",m,p->next);
}
/*-------<<
function defination for reverse >>------*/
void
reverse()
{
node *p,*q,*r;
p=NULL;
q=head;
r=q->next;
while(q!=NULL)
{
q->next=p;
p=q;
q=r;
if(q!=NULL)
r=q->next;
}
printf("\n\nCreated link
list==>\n\n");
printf("head--->");
for(q=p;q!=NULL;q=q->next)
{
printf("%d--->",(q->data));
}
printf("NULL");
}
/*------------<< Main Function Start >>---------------*/
void
main()
{
char ans;
//Variable declaration
do
{
clrscr();
//Clear the text mode of window
printf("\n\t\t\t----<< MENU
>>----");
printf("\n\n\t\t\t1.Create");
printf("\n\t\t\t2.Insert");
printf("\n\t\t\t3.Delete");
printf("\n\t\t\t4.Search");
printf("\n\t\t\t5.Reverse");
printf("\n\t\t\t6.Display");
printf("\n\t\t\t7.Exit");
printf("\n\n\t\t\t-------------------------");
printf("\n\n\t\t\tEnter your
choice==>");
scanf("%d",&ch); //accept
choice from user
switch(ch)
{
case 1:
create();
display();
break;
case 2:
create();
display();
do
{
printf("\n\n\tInsert:");
printf("\n\t\t1.Beginning");
printf("\n\t\t2.In
Between");
printf("\n\t\t3.End");
printf("\n\t\t4.Exit");
printf("\n\n\t\tEnter your
choice==>");
scanf("%d",&ch);
switch(ch)
{
case 1:
insert_beginning();
display();
break;
case 2:
insert_between();
display();
break;
case 3:
insert_end();
display();
break;
case 4:
exit(0);
break;
}
printf("\n\n\n\t\tDo you want to
continue(Y/N)?==>");
ans=getch();
}while(ans=='Y'||ans=='y');
break;
case 3:
create();
display();
do
{
printf("\n\n\tDelete:");
printf("\n\t\t1.Beginning");
printf("\n\t\t2.In
Between");
printf("\n\t\t3.End");
printf("\n\t\t4.Exit");
printf("\n\n\t\tEnter your
choice==>");
scanf("%d",&ch);
switch(ch)
{
case 1:
delete_beginning();
display();
break;
case 2:
delete_between();
display();
break;
case 3:
delete_end();
display();
break;
case 4:
exit(0);
break;
}
printf("\n\n\n\t\tDo you want to
continue(Y/N)?==>");
ans=getch();
}while(ans=='Y'||ans=='y');
break;
case 4:
create();
display();
search();
break;
case 5:
create();
display();
reverse();
break;
case 6:
create();
display();
break;
case 7:
exit(0);
break;
default:
printf("\n\tinvalid choice");
break;
}
printf("\n\n\n\t\tDo you want to
continue(Y/N)?==>");
ans=getch();
}while(ans=='Y'||ans=='y');
getch();
}
/*-----------<< End Of Main Function >>--------------*/
/*-----------------<< OUTPUT
SCREEN >>-------------------*/
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
------------------------
Enter your
choice==>1
Enter
no. of nodes==>3
Enter
values of node==>10
Enter
values of node==>20
Enter
values of node==>30
Created
link list==>
head--->10--->20--->30--->NULL
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your choice==>2
Enter
no. of nodes==>2
Enter
values of node==>10
Enter
values of node==>20
Created
link list==>
head--->10--->20--->NULL
Insert:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>1
Enter
the value to be inserted==>30
Created
link list==>
head--->30--->10--->20--->NULL
Do you want to
continue(Y/N)?==>Y
Insert:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>2
Enter
the value to be inserted==>40
Enter
the previous node==>30
Created
link list==>
head--->30--->40--->10--->20--->NULL
Do you want to continue(Y/N)?==>Y
Insert:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>3
Enter
the value to be inserted==>50
Created
link list==>
head--->30--->40--->10--->20--->50--->NULL
Do you want to
continue(Y/N)?==>N
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your
choice==>3
Enter
no. of nodes==>6
Enter
values of node==>10
Enter
values of node==>20
Enter
values of node==>30
Enter
values of node==>40
Enter
values of node==>50
Enter
values of node==>60
Created
link list==>
head--->10--->20--->30--->40--->50--->60--->NULL
Delete:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>1
Created
link list==>
head--->20--->30--->40--->50--->60--->NULL
Do you want to
continue(Y/N)?==>Y
Delete:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>2
Enter
the data to be deleted : 30
Created
link list==>
head--->20--->40--->50--->60--->NULL
Do you want to
continue(Y/N)?==>Y
Delete:
1.Beginning
2.In Between
3.End
4.Exit
Enter your choice==>3
Created
link list==>
head--->20--->40--->50--->NULL
Do you want to
continue(Y/N)?==>N
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your
choice==>4
Enter
no. of nodes==>4
Enter
values of node==>10
Enter
values of node==>20
Enter
values of node==>30
Enter
values of node==>40
Created
link list==>
head--->10--->20--->30--->40--->NULL
Enter
the data to be searched: 30
Found at location=3
Searched link is 30->2960
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your choice==>5
Enter
no. of nodes==>4
Enter
values of node==>10
Enter
values of node==>20
Enter
values of node==>30
Enter
values of node==>40
Created
link list==>
head--->10--->20--->30--->40--->NULL
Created
link list==>
head--->40--->30--->20--->10--->NULL
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your
choice==>6
Enter
no. of nodes==>3
Enter
values of node==>10
Enter
values of node==>20
Enter
values of node==>30
Created
link list==>
head--->10--->20--->30--->NULL
Do you want to
continue(Y/N)?==>Y
----<< MENU
>>----
1.Create
2.Insert
3.Delete
4.Search
5.Reverse
6.Display
7.Exit
-------------------------
Enter your choice==>7
No comments:
Post a Comment