Saturday, January 16, 2021

Deletion Of All Nodes From A Doubly Linked List

 
# include < stdio.h>
# include < stdlib.h>
struct link
{
char info[20];
struct link *next;
};
struct link *start,*p,*node;
int i=1;
void create()
{
char ch='y';
node=start;
while(ch != 'n')
{
node->next = (struct link* ) malloc(sizeof(struct link));
node = node->next;
printf("\n Enter string for node: %d: ",i);
scanf("%s", node->info);
node->next = NULL;
i++;
printf("\n Enter choice-- 'n' for break: ");
ch = getche();
}
}
void display(struct link *n)
{
node=n;
while(node)
{
printf("\n 0x%x ", node);
printf("%s\n",node->info);
node=node->next;
}
}
void delNodes(struct link *n)
{
node=n;
p = n->next;

while(p)
{

node->next = p->next;
printf("Deleting %s\n",p->info);
free(p);
p = node->next;
}
do
{
free(start);
start=start->next;
}while(start);
}

void main()
{
clrscr();
start = (struct link* ) malloc(sizeof(struct link));
printf("\n Enter string for node: %d: ",i);
scanf("%s", start->info);
start->next = NULL;
create();
printf("\nAfter Creation...");
display(start);
delNodes(start);
printf("\nAfter Deletion...");
display(start);
getch();

}


No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner