Tuesday, October 4, 2011

Data Structure Lab Programs -Linked list implementation .

Practical # 8

 

/*Linked list implementation */

 

#include<stdio.h>

#include<conio.h>

#include<process.h>

struct Node

{

   int info;

   Node *next;

}*st,*nptr,*sv,*ptr,*rear;

Node *Crt_Nw_Node(int n)

{

ptr=new Node;

ptr->info=n;

ptr->next=NULL;

return ptr;

}

//insert node at front

void insert_beg(Node *np)

{

if(st==NULL)//list is impty

st=np;

else

{

sv=st;

st=np;

np->next=sv;

}

}

//insert node at end of list

void Display(Node *np)

{

while(np!=NULL)

{

printf("%d",np->info,"->");

np=np->next;

}

printf("!!!!!!!!\n");

}

// Insert node at end of list

void insert_end(Node *np)

{

if(st==NULL)

{

st=np;

rear=np;

}

else

{

rear->next=nptr;

rear=nptr;

}

}

void del_beg()

{

if(st==NULL)

{

printf("\n UNDERFLOW!!!!!\n");

exit(-1);

}

else

{

ptr=st;

st=st->next;

printf("\n Element deleted is :%d",ptr->info);

delete ptr;

}

}

//delete node from end of list

void del_end()

{

if(st==NULL)

{

printf("\nUNDERFLOW!!!!!\n");

exit(-1);

}

else if(st->next==NULL)

{

ptr=st;

st=NULL;

printf("\n Element deleted is%d",ptr->info);

delete ptr;

}

else

{

rear=st;

ptr=st->next;

while(ptr->next==NULL)

{

rear=ptr;

ptr=ptr->next;

}

rear->next=NULL;

printf("\n Element deleted is:%d",ptr->info);

delete ptr;

}

}

 

void main()

{

st=NULL;

int info;

int ch1;

char ch='y';

while(ch=='y'||ch='Y')

{

clrscr();

printf("\n Enter information for the new node...");

scanf("%d",info);

printf("\n Creating New Node!! Press enter to continue...");

getch();

nptr=Crt_Nw_Node(info);

if(nptr!=NULL)

{

printf("\n\n New Node Created Successfully.Press enter to continue..");

getch();

}

else

{

printf("\n Cannot Create New Node!!!Aborting!!\n");

getch();

exit(1);

printf("\n 1. To Enter At Beginning");

printf("\n 2. To enter At end");

printf("\n 3.To Delete from Beginning");

printf("\n 4.To deleted from end");

printf("\nEnter Your Choice ");

scanf("%d",ch1);

switch(ch1)

{

case 1:

printf("\n Now Inserting this node at the beginning of the list...");

printf("\n Press Enter to continue...\n");

getch();

insert_beg(nptr);

printf("\n Now the list is:\n");

Display(st);

printf("\n Press Y to continue");

scanf("&d",ch);

break;

 

case 2:

printf("\n Now Inserting this node at the end of the list...");

printf("\n Press Enter to continue...\n");

getch();

insert_end(nptr);

printf("\n Now the list is:\n");

Display(st);

printf("\n Press Y to continue");

scanf("&d",ch);

break;

 

case 3:

printf("\n The list now is :\n");

Display(st);

getch();

del_beg();

printf("\n Press Y to continue");

scanf("&d",ch);

break;

case 4:

printf("\n The list now is :\n");

Display(st);

getch();

del_end();

printf("\n Press Y to continue");

scanf("&d",ch);

break;

 

default:

printf("\n Err!!!!!!");

break;

}

}

}

 

 

Output:-

 

Enter information for the new node… 34

 

Creating New Node!! Press enter to continue….

 

New Node Creating Successfully.Press enter to continue…

  1. To Enter At Beginning
  2.  To Enter At End
  3. To Delete From Beginning
  4. To Delete From End

Enter your choice 1

 

Now inserting this node at the beginning of the list…

Press Enter to continue…

Now the list is :

34 -> !!!!!!!!!!!

 

Press Y to continue Y

 

 

 

Enter information for the new node… 23

 

Creating New Node!! Press enter to continue….

 

New Node Creating Successfully.Press enter to continue…

1.To Enter At Beginning

2. To Enter At End

3.To Delete From Beginning

4.To Delete From End

Enter your choice 1

 

Now inserting this node at the beginning of the list…

Press Enter to continue…

Now the list is :

23->34 -> !!!!!!!!!!!

 

Press Y to continue Y

 

 

Enter information for the new node… 34

 

Creating New Node!! Press enter to continue….

 

New Node Creating Successfully.Press enter to continue…

1.To Enter At Beginning

2. To Enter At End

3.To Delete From Beginning

4.To Delete From End

Enter your choice 1

 

Now inserting this node at the beginning of the list…

Press Enter to continue…

Now the list is :

34 -> 23 -> 34 ->  !!!!!!!!!!!

Press Y to continue Y

 

 

 

Enter information for the new node… 45

 

Creating New Node!! Press enter to continue….

 

New Node Creating Successfully.Press enter to continue…

1.To Enter At Beginning

2. To Enter At End

3.To Delete From Beginning

4.To Delete From End

Enter your choice 3

 

Now inserting this node at the beginning of the list…

Press Enter to continue…

Now the list is :

34 ->23 -> 34 ->  !!!!!!!!!!!

 

Element deleted is :34

 

Press Y to continue N

 

 

No comments :

Post a Comment