#include<stdio.h>
#include<stdlib.h>
struct nod
{
int data;
struct nod *link;
};
typedef struct nod *node;;
node insert_front(node head,int data)
{
node temp;
temp=(node)malloc(sizeof(node));
temp->data=data;
temp->link=head->link;
head->link=temp;
return head;
}
node delete_rear(node head)
{
node temp,prev;
if(head->link==head)
{
printf("\n empty\n");
return head;
}
prev=head;
temp=head->link;
while(temp->link!=head)
{
prev=temp;
temp=temp->link;
}
prev->link=head;
printf("\n the deleted node is %d",temp->data);
free(temp);
return head;
}
void display(node head)
{
node temp;
temp=head->link;
printf("head->");
while(temp!=head)
{
printf("%d\n ",temp->data);
temp=temp->link;
}
}
main()
{
int num,choice;
node head;
head=(node)malloc(sizeof(node));
head->link=head;
do
{
printf("\n MENU \n1.insert at front\n2.delete at rear\n3.display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n enter the element\n");
scanf("%d",&num);
head=insert_front(head,num);
break;
case 2:
head=delete_rear(head);
break;
case 3:
display(head);
break;
}
}while(choice<4);
}
#include<stdlib.h>
struct nod
{
int data;
struct nod *link;
};
typedef struct nod *node;;
node insert_front(node head,int data)
{
node temp;
temp=(node)malloc(sizeof(node));
temp->data=data;
temp->link=head->link;
head->link=temp;
return head;
}
node delete_rear(node head)
{
node temp,prev;
if(head->link==head)
{
printf("\n empty\n");
return head;
}
prev=head;
temp=head->link;
while(temp->link!=head)
{
prev=temp;
temp=temp->link;
}
prev->link=head;
printf("\n the deleted node is %d",temp->data);
free(temp);
return head;
}
void display(node head)
{
node temp;
temp=head->link;
printf("head->");
while(temp!=head)
{
printf("%d\n ",temp->data);
temp=temp->link;
}
}
main()
{
int num,choice;
node head;
head=(node)malloc(sizeof(node));
head->link=head;
do
{
printf("\n MENU \n1.insert at front\n2.delete at rear\n3.display\n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("\n enter the element\n");
scanf("%d",&num);
head=insert_front(head,num);
break;
case 2:
head=delete_rear(head);
break;
case 3:
display(head);
break;
}
}while(choice<4);
}
No comments:
Post a Comment