Friday, 22 June 2012

write a c program to create a signly linked list, display it and erase complete list


/*write a c program to create a signly linked list, display it and
erase complete list*/
#include<stdio.h>
#include<conio.h>
#include<alloc.h>
struct node
{
int data;
struct node *next;
}*start,*q,*temp;
void create();
void display();
void delete();
void main()
{
char ch='y';
int choice;
clrscr();
while(ch=='y'||ch=='Y')
{
create ();
printf("\ndo you want to continue(Y|N):-");
scanf("\n%s",&ch);
}
display();
delete();
display();
getch();
}
void create()
{
temp=malloc(sizeof(struct node));
printf("\nenter the number:-");
scanf("%d",&temp->data);
temp->next=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void display()
{
if(start==NULL)
printf("\nlinked list is empty");
else
{
printf("\nelement in singly linked list are:-\n");
q=start;
while(q!=NULL)
{
printf("%d\n",q->data);
q=q->next;
}
}
}
void delete()
{
while(start!=NULL)
{
start=start->next;
}
}
/*
enter the number:-2                                                          
                                                                             
do you want to continue(Y|N):-y                                              
                                                                             
enter the number:-5                                                          
                                                                             
do you want to continue(Y|N):-y                                              
                                                                             
enter the number:-7                                                          

do you want to continue(Y|N):-y                                              
                                                                             
enter the number:-8                                                          
                                                                             
do you want to continue(Y|N):-y                                              
                                                                             
enter the number:-1                                                          
                                                                             
do you want to continue(Y|N):-n                                              

element in singly linked list are:-                                          
2                                                                            
5                                                                            
7                                                                            
8                                                                            
1                                                                            
                                                                             
linked list is empty                                                          

*/

No comments: