#include<stdio.h>
#include<conio.h>
struct NODE
{
int data;
struct NODE *next;
};
typedef struct NODE node;
node *head, *end;
int count=0;
void insert_end();
void display();
void delete_end();
void main()
{
int ch;
clrscr();
do
{
printf("\n1. Insert Node at End");
printf("\n2. Display");
printf("\n3. Delete Node from End");
printf("\n4. Exit ");
printf("\n\nEnter your choice: ");
scanf("%d", &ch);
switch(ch)
{
case 1:
insert_end();
break;
case 2:
display();
getch();
break;
case 3:
delete_end();
break;
case 4:
break;
default:
printf("\n Please Enter Proper Choice");
break;
}
}while(ch != 4);
}
void insert_end()
{
node *temp;
temp=(node *)malloc(sizeof(node));
printf("\n Enter value for new node: ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL)
head=temp;
end->next=temp;
end=temp;
count++;
}
void display()
{
node *temp;
if(count == 0)
printf("\n Please insert node first \n");
else
{
printf("\n You have inserted %d nodes",count);
printf("\n The next list as: ");
for(temp=head;temp !=NULL;temp=temp->next)
{
printf(" %d ",temp->data);
}
}
}
void delete_end()
{
node *temp;
int i;
if(count == 0)
printf("\n Please insert a node \n");
else
{
for(temp=head,i=1;i<count-1;temp=temp->next,i++);
temp->next=NULL;
printf("\n\n You have Deleted node %d\n\n", end->data);
free(end);
end=temp;
count--;
}
}
No comments:
Post a Comment