Wednesday, May 6, 2009

PROGRAM FOR EVELUATING SINGLE LINKED LIST

#include
#include
#include
#define NULL 0
struct slist
{
int info;
struct slist *next;
};
typedef struct slist node;
node *root,*nu,*prev,*list,*start,*temp;
int ele;
void create()
{
printf("Enter -1 to stop\n");
scanf("%d",&ele);
while(ele!=-1)
{
nu=(node*)malloc(sizeof(node));
nu->info=ele;
nu->next=NULL;
if(root==NULL)
{
root=nu;
prev=nu;
}
else
{
prev->next=nu;
prev=nu;
}
scanf("%d",&ele);
}
}
void display()
{
list=root;
if(root==NULL)
printf("The list is empty\n");
else
{
printf("\n Contents of list");
while(list!=NULL)
{
printf("%d,",list->info);
list=list->next;
}
printf("NULL\n");
}
}
void deletion()
{
int item;
node *p;
printf("Enter the element to be deleted\n");
scanf("%d",&item);
if(root->info==item)
{
p=root;
root=root->next;
free(p);
}
else
{
temp=root;
while((item!=temp->info)&&(temp!=NULL))
{
prev=temp;
temp=temp->next;
}
if(item==temp->info)
{
prev->next=temp->next;
free(temp);
}
}
}
void main()
{
int ch;
root=NULL;
clrscr();
do
{
printf("\n Press 1 for entering elements into list\n Press 2 to delete an element from the list\n Press 3 to exit\n");
printf(" Enter your choice\n");
scanf("%d",&ch);
switch(ch)
{
case 1:create();
display();
break;
case 2:deletion();
display();
break;
case 3:exit(0);
break;
}
}while(ch!=3);
}

No comments:

Post a Comment