Tuesday, July 28, 2009

singly linked list -- Data structure Pgm

//singly linked list
#include
#include
void create();
void insert_first();
void delete_first();
void modify();
struct node* getnode();
void view();
void readnode(struct node *newnode);
struct node{ int data; struct node *link;};
struct node *head,*temp;
struct node* getnode()
{
struct node *newnode;
int size;
size=sizeof(struct node);
newnode=(struct node*)malloc(size);
getch(); return (newnode);
}
void readnode(struct node *newnode)
{
printf("\n\nenter the data:->");
scanf("%d",&newnode->data);
newnode->link=NULL; return;
}
void release_node(struct node *del)
{
free(del);
return;
}
void create()
{
struct node *newnode;
char ch;
do
{
newnode=getnode();
readnode(newnode);
if(head==NULL)
{
head=newnode;
temp=newnode;
}
else
{
temp->link=newnode;
temp=temp->link;
}
printf("\n\nDo u want to continue(Y/N):->");
scanf("%s",&ch);
}
while((ch=='y')(ch=='Y'));
return;
}
void insert_first()
{
struct node *newnode;
newnode=getnode();
readnode(newnode);
if(head==NULL)
head=newnode;
else
{
newnode->link=head;
head=newnode;
}
view();
return;
}
void delete_first()
{
struct node *del;
if(head==NULL)
printf("\n\nList is Empty");
else
{
del=head; head=head->link;
printf("\n\nThe deleted data is %d",del->data); release_node(del);
}
view();
return;
}
void view()
{
struct node *disp;
if (head==NULL)
printf("\n\nThe list is empty");
else
{
printf("\n\n");
for(disp=head;disp!=NULL;disp=disp->link)
printf("%d\t",disp->data); } getch();
return;
}
void modify()
{
int temp;
struct node *disp;
if (head==NULL)
printf("\n\nThe list is empty");
else
{
printf("\nEnter the data to be changed:-");
scanf("%d",&temp);
printf("\n\n");
for(disp=head;disp!=NULL;disp=disp->link)
{
if(temp==disp->data)
{ printf("\nEnter the new data");
scanf("%d",&disp->data);
printf("\ndata %d modified as %d",temp,disp->data);
}
}
}
getch();
return;
}
void main()
{
int c;
clrscr();
printf("1.Create list\n2.insert first\n3.delete first\n4.Modify data\n5.view list\n6.exit\n");
do
{
printf("\n\nEnter ur choice(1-6):->");
scanf("%d",&c); switch (c) { case 1: create(); break; case 2: insert_first(); break;
case 3: delete_first(); break; case 4: modify(); break; case 5: view(); break;
default: exit(0); }
}while(1);
}
_______________________*ALL THEBEST*_____________________________


With Regards,
5stararun.....

No comments:

Post a Comment