PROGRAM 1:
PROGRAM 2:
PROGRAM TO IMPLEMENT SINGLY LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node *link;
};
typedef struct node *NODE;
NODE getnode();
void free_node(NODE X);
NODE insert_front(int item,NODE
first);
void display(NODE first);
NODE del_front(NODE first);
void main()
{
NODE first=NULL;
int choice,item;
for(;;)
{
printf("1.INSERT:\n");
printf("2.DELETE:\n");
printf("3.DISPLAY\n");
printf("4.EXIT:\n");
printf("enter your
choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("ENTER THE
ITEM TO BE INSERTED IN THE LIST\n");
scanf("%d",&item);
first=insert_front(item,first);
break;
case 2: first=del_front(first);
break;
case 3: display(first);
break;
default: exit(0);
}
}
}
NODE getnode()
{
NODE X;
X=(NODE)malloc(sizeof(struct
node));
if(X==NULL)
{
printf("************OUT OF
MEMORY*****************\n");
exit(0);
}
return X;
}
void free_node(NODE X)
{
free(X);
}
NODE insert_front(int item,NODE
first)
{
NODE temp;
temp=getnode();
temp->info=item;
temp->link=first;
return temp;
}
void display(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("*************LIST
UNDERFLOW************\n");
return;
}
printf("************CONTETS
OF SINGLE LIST*******:\n");
temp=first;
while(temp!=NULL)
{
printf("%d\n",temp->info);
temp=temp->link;
}
printf("\n");
}
NODE del_front(NODE first)
{
NODE temp;
if(first==NULL)
{
printf("************EMPTY
LIST CANNOT DELETE************\n");
return first;
}
temp=first;
temp=temp->link;
printf("**********DELETED
ITEM IS =%d********\n",first->info);
free_node(first);
return temp;
}
PROGRAM TO IMPLEMENT DOUBLY
LINKED LIST
#include<stdio.h>
#include<stdlib.h>
struct node
{
int
info;
struct
node *llink;
struct
node *rlink;
};
typedef struct node* NODE;
NODE getnode()
{
NODE x;
x=((NODE)malloc(sizeof(struct
node)));
if (x==NULL)
{
printf("out
of memory\n");
exit(0);
}
return x;
}
void freenode(NODE x)
{
free(x);
}
NODE insert_front(int item, NODE
head)
{
NODE
temp, cur;
temp=getnode();
temp->info=item;
cur=head->rlink;
head->rlink=temp;
temp->llink=head;
temp->rlink=cur;
cur->llink=temp;
return
head;
}
NODE insert_rear(int item, NODE
head)
{
NODE
temp, cur;
temp=getnode();
temp->info=item;
cur=head->llink;
head->llink=temp;
temp->rlink=head;
temp->llink=cur;
cur->rlink=temp;
return
head;
}
NODE delete_front(NODE head)
{
NODE
cur, next;
if(head->rlink==head)
{
printf("list is empty\n");
return
head;
}
cur=head->rlink;
next=cur->rlink;
head->rlink=next;
next->llink=head;
printf("deleted
node is: %d\n", cur->info);
freenode(cur);
return
head;
}
NODE delete_rear(NODE head)
{
NODE
cur, prev;
if(head->llink==head)
{
printf("list is empty\n");
return
head;
}
cur=head->llink;
prev=cur->llink;
head->llink=prev;
prev->rlink=head;
printf("deleted
node is: %d\n", cur->info);
freenode(cur);
return
head;
}
void display(NODE head)
{
NODE
temp;
if(head->rlink==head)
{
printf("list is empty\n");
return;
}
printf("contents
of list:\n");
temp=head->rlink;
while(temp!=head)
{
printf("%d\t",
temp->info);
temp=temp->rlink;
}
printf("\n");
}
void main()
{
NODE
head ;
int
ch, item;
head=getnode();
head->rlink=head;
head->llink=head;
do
{
printf("1:INSERT_FRONT\n");
printf("2:INSERT_REAR\n");
printf("2:DELETE_FRONT\n");
printf("4:DELETE_REAR\n");
printf("5:DISPLAY\n");
printf("6:EXIT\n");
printf("enter your choice\n");
scanf("%d", &ch);
switch(ch)
{
case
1:
printf("*******ENTER
THE ITEM TO BE INSERTED AT FRONT END:******\n");
scanf("%d",
&item);
head=insert_front(item,
head);
break;
case
2:
printf("***********ENTER
THE ITEM TO BE INSERTED AT REAR END:*********");
scanf("%d",
&item);
head=insert_rear(item,
head);
break;
case
3:
head=delete_front(head);
break;
case
4:
head=delete_rear(head);
break;
case
5:
display(head);
break;
case
6:
exit(0);
break;
default
:
printf("*******INVALID
CHOICE***********\n");
break;
}
}while(ch!=6);
}
No comments:
Post a Comment
we are hear to discuss your queries ,so please feel free to ask any of your queries.