THIS PROGRAM DELETES THE LAST NODE FROM DOUBLY LINKED LIST
#include<stdio.h>
#include<malloc.h>
struct dlist
{
int info;
struct dlist *link;
struct dlist *prev;
};
int i ;
struct dlist start, *prev;
void del_last (struct dlist *);
void create_dlist (struct dlist *);
void display (struct dlist *);
/* Function to create doubly linked list */
void create_dlist(struct dlist *ptr)
{
int n;
start.link = NULL; /* Empty list */
start.prev = NULL;
ptr = &start; /* Ptr points to the start pointer */
printf("\n How many nodes you want to create: ");
scanf("%d",&n);
printf("\n\n ==============================================================================\n");
printf("\n Note That:\n -----------\n Enter numeric data in nodes\n");
printf("\n ==============================================================================\n");
while( i!=n)
{
ptr->link = (struct dlist *) malloc(sizeof(struct dlist));
ptr->link->prev = ptr;
ptr = ptr->link;
printf("\n Enter the values of the node : %d: ", (i+1));
scanf("%d", &ptr->info);
ptr->link = NULL;
fflush(stdin);
i++;
}
printf("\n\n --------------------");
printf("\n Total nodes = %d",i);
printf("\n --------------------\n\n");
}
/* Function to delete last node */
void del_last(struct dlist *ptr)
{
ptr = start.link;
prev = &start;
if (ptr == NULL)
{
printf("\n\n\n ----------");
printf("\n Underflow");
printf("\n ----------");
}
else
while(ptr->link)
{
ptr = ptr->link;
prev = prev->link;
}
prev->link = ptr->link;
free(ptr);
}
/* Function to display the list */
void display(struct dlist *ptr)
{
ptr = start.link;
while (ptr)
{
printf("\n\n\t\t%d", ptr->info);
ptr = ptr->link;
}
}
/* Main Function */
void main()
{
struct dlist *ptr = (struct dlist *) malloc(sizeof(struct dlist));
clrscr();
create_dlist (ptr);
printf("\n Doubly linked list is:\n");
printf("\n ----------------------");
display(ptr);
del_last(ptr);
printf("\n\n -----------------------------------------------");
printf("\n Doubly linked list after deletion of last node:");
printf("\n -----------------------------------------------");
display(ptr);
getch();
}
Comments
Post a Comment