Algorithm to delete and display elements in a singly linked list
Answers
Answer:
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int info;
struct node *link;
};
struct node *FIRST;
void createlist()
{
char ch;
printf("Enter n for break:\n");
scanf("%c",&ch);
while(ch!='n')
{
struct node *NEW_NODE,*SAVE;int x;
NEW_NODE=(struct node *)malloc(sizeof(struct node));
printf("Enter Data:");
scanf("%d",&x);
NEW_NODE->info=x;
if(FIRST==NULL)
{
NEW_NODE->link=NULL;
FIRST=NEW_NODE;
}
else
{
SAVE=FIRST;
while(SAVE->link!=NULL)
{
SAVE=SAVE->link;
}
SAVE->link=NEW_NODE;
NEW_NODE->link=NULL;
}
fflush(stdin);
printf("Enter n for break:\n");
scanf("%c",&ch);
}
}
void delfirst()
{
struct node *SAVE;
if (FIRST==NULL)
printf("Linked List is Empty");
else if(FIRST->link==NULL)
{
printf("Deleted element is %d",FIRST->info);
FIRST=NULL;
}
else
{
printf("Deleted element is %d",FIRST->info);
FIRST=FIRST->link;
}
}
void display()
{
struct node *SAVE;
if(FIRST==NULL)
{
printf("sll is empty\n");
return;
}
printf("elements are:\n");
SAVE=FIRST;
while(SAVE!=NULL)
{
if(SAVE->link==NULL)
printf("|%d|",SAVE->info);
else
printf("|%d|->",SAVE->info);
SAVE=SAVE->link;
}
printf("\n");
return;
}
void main()
{
clrscr();
FIRST=NULL;
createlist();
display();
delfirst();
display();
getch();
}
Answer:
this is hilarious.........