Google Search

Saturday, January 28, 2012

C PROGRAM TO IMPLEMENT LINKED LIST ALGORITHM


ALGORITHM TO IMPLEMENT LINKED LIST 

1.  t = newmode( )
2.  Enter info to be inserted
3.  Read n
4.  t   info = n
5.  t   next = start
6.  Start = t

 INSERTION
BEGIN
1.  t   next = start
2.  start = t
     Return
MIDDLE
1.  Enter info of the node after which new node to be inserted 
2.  Read x
3.  p = start
4.  Repeat step 5 until p   info < > x 
5.  p = p   next
6.  t   next = p   next
7.  p   next = t
8.  Return

LAST
1.  p = start
2.  Repeat step 3 until p   next NULL
3.  p = p   next
4.  t   next = NULL
5.  p   next = t
6.  Return

DELETION
BEGIN
1.  x = start
2.  start = start   next
3.  delnode(x)


MIDDLE
1.  Enter th e info of node to be deleted
2.  Read n
3.  p = start
4.  c = start
5.  while (c   info < > NULL)
     p = c
     c = c   next
6.  p   next = c   next
7. delnode ( c )
8.  Return



LAST
1.  p = start
     c = start
2.  while (c next < > NULL)
     p = c
     c = c next
3.  p   next = c   next
4.  delnode ( c)
5.  Return

TRAVERSAL
1.  p = start
2.  while (p < > NULL)
     Print p   info
     P = p   next
3.  Return






// linked list//

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct node
{
int info;
struct node *next;
};
typedef struct node NODE;
NODE *start;
void createmptylist(NODE **start)
{
*start=(NODE *)NULL;
}
void traversinorder(NODE *start)
{
while(start != (NODE *) NULL)
{
printf("%d\n",start->info);
start=start->next;
}
}
void insertatbegin(int item)
{
NODE *ptr;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
if(start==(NODE *)NULL)
ptr->next=(NODE *)NULL;
else
ptr->next=start;
start=ptr;


}
void insert_at_end(int item)
{
NODE *ptr,*loc;
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=(NODE *)NULL;
if(start==(NODE*)NULL)
start=ptr;
else
{
loc=start;
while(loc->next!=(NODE *)NULL)
loc=loc->next;
loc->next=ptr;
}
}
void insert_spe(NODE *start,int item)
{
NODE *ptr,*loc;
int temp,k;
for(k=0,loc=start;k<temp;k++)
{
loc=loc->next;
if(loc==NULL)
{
printf("node in the list at less than one\n");
return;
}
}
ptr=(NODE *)malloc(sizeof(NODE));
ptr->info=item;
ptr->next=loc->next;;
loc->next=ptr;
}
void main()
{
int choice,item,after;
char ch;
clrscr();
createmptylist(start);
do
{
printf("1.Insert element at begin \n");
printf("2. insert element at end positon\n");
printf("3. insert specific the position\n");
printf("4.travers the list in order\n");
printf("5. exit\n");
printf("enter your choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1: printf("Enter the item\n");
 scanf("%d",&item);
 insertatbegin(item);
  break;
case 2:  printf("Enter the item\n");


 scanf("%d",&item);
 insert_at_end(item);
  break;
case 3: printf("Enter the item\n");
 scanf("%d",&item);
 insert_spe(start,item);
  break;
case 4: printf("\ntravers the list\n");
 traversinorder(start);
  break;
case 5: return;
}
fflush(stdin);
printf("do your want continous\n");
scanf("%c",&ch);
}while((ch='y')||(ch='y'));
getch();

0 comments:

Post a Comment