CURSOR IMPLEMENTATION OF LIST ADT


SOURCE CODE:

“curslist.h” File:

#include<stdio.h>
#include<conio.h>
#define SPACE_SIZE 10
struct Node
{
   int data;
   int Next;
};
typedef int PtrToNode;
typedef PtrToNode POSITION;
typedef PtrToNode LIST;
struct Node cursor[SPACE_SIZE];

void InitializeCursor()
{
   int i;
   for(i=0;i<=SPACE_SIZE-1;i++)
   {
      cursor[i].Next=i+1;
      cursor[i].data=0;
   }
      cursor[SPACE_SIZE-1].Next=-1;
}
POSITION CursorAlloc()
{
   POSITION P;
   P=cursor[0].Next;
   cursor[0].Next=cursor[P].Next;
   cursor[P].data=-1;
   cursor[P].Next=-1;
   return P;
}
void CursorFree(POSITION P)
{
   cursor[P].Next=cursor[0].Next;
   cursor[0].Next=P;
   cursor[P].data=0;
}
void Insert(int X,POSITION P)
{
      POSITION Temp;
      Temp=CursorAlloc();
      if(Temp==-1)
     printf("\nOut of space");
      else if(cursor[P].data==0)
     printf("\nPosition is not in the list");
      else
      {

     cursor[Temp].data=X;
     cursor[Temp].Next=cursor[P].Next;
     cursor[P].Next=Temp;
      }
   }
int IsLast(POSITION P)
{
   return cursor[P].Next==-1;
}
int IsEmpty(LIST L)
{
   return cursor[L].Next==-1;
}
POSITION Find(int X,LIST L)
{
   POSITION Temp;
   Temp=cursor[L].Next;
   while(Temp!=-1&&cursor[Temp].data!=X)
   Temp=cursor[Temp].Next;
   return Temp;
}
POSITION FindPrevious(int X,LIST L)
{
   POSITION Temp;
   Temp=L;
   while(Temp!=-1&&cursor[cursor[Temp].Next].data!=X)
   Temp=cursor[Temp].Next;
   return Temp;
}
void Delete(int X,LIST L)
{
   POSITION P,Temp;
   P=FindPrevious(X,L);
   if(!IsLast(P))
   {
      Temp=cursor[P].Next;
      cursor[P].Next=cursor[Temp].Next;
      CursorFree(Temp);
   }
}
void MakeEmpty(LIST L)
{
   while(!IsEmpty(L))
   Delete(cursor[cursor[L].Next].data,L);
}
void Display()
{
   int i;
   for(i=0;i<=SPACE_SIZE-1;i++)
   printf("\n%d\t%d\t%d",i,cursor[i].data,cursor[i].Next);
}


“curslist.c” File:

#include<stdio.h>
#include<conio.h>
#include"curslist.h"
void main()
{
LIST L=-1;
POSITION P;
int choice,place,x;
clrscr();
printf("\n1.Create\n2.Insert\n3.Delete\n4.MakeEmpty\n5.Display\n6.Find\n7.Exit");
A:
printf("\nEnter ur choice:\t");
scanf("%d",&choice);
switch(choice)
{
case 1:
    if(L==-1)
    {
       InitializeCursor();
       L=CursorAlloc();
    }
    else
       printf("\nList is already created");
    break;
case 2:
    if(L==-1)
       printf("\nList is not yet initialized");
    else
    {
       printf("\nWhere u want to insert?");
       scanf("%d",&place);
       printf("\nEnter the element to insert");
       scanf("%d",&x);
       Insert(x,place);
    }
    break;
case 3:
    if(L==-1)
       printf("\nList is not yet initialized");
    else
    {
       printf("\nWhich element you want to delete?");
       scanf("%d",&x);
       Delete(x,L);
    }
    break;
case 4:
    if(L==-1)
       printf("\nList is not yet initialized");
    else
       MakeEmpty(L);
    break;
case 5:
    if(L==-1)
       printf("\nList is not yet initialized");
    else
       Display();
    break;
case 6:
    if(L==-1)
       printf("\nList is not yet initialized");
    else
    {
       printf("\nWhich element you want to search?");
       scanf("%d",&x);
       P=Find(x,L);
       printf("\nThe element is at %d",P);
    }
    break;
case 7:
    exit(0);
default:
    printf("\n *******WRONG ENTRY*******");
}
goto A;
}

OUTPUT:

1.Create
2.Insert
3.Delete
4.MakeEmpty
5.Display
6.Find
7.Exit
Enter ur choice:    1

Enter ur choice:    5
0       0       2
1       -1      -1
2       0       3
3       0       4
4       0       5
5       0       6
6       0       7
7       0       8
8       0       9
9       0       -1

Enter ur choice:    2
Where u want to insert?    1
Enter the element to insert:    100

Enter ur choice:        5
0       0       3
1       -1      2
2       100     -1
3       0       4
4       0       5
5       0       6
6       0       7
7       0       8
8       0       9
9       0       -1

Enter ur choice:    2
Where u want to insert?    2
Enter the element to insert:    200

Enter ur choice:    2
Where u want to insert?    3
Enter the element to insert:    300

Enter ur choice:    5
0       0       3
1       -1      2
2       100     -1
3       200       4
4       300       5
5       0       6
6       0       7
7       0       8
8       0       9
9       0       -1

Enter ur choice:    6
Which element you want to search?    200
The element is at    3

Enter ur choice:    3
Which element you want to delete:    200

Enter ur choice:    5
0       0       3
1       -1      2
2       100     4
3       0       5
4       300     -1
5       0       6
6       0       7
7       0       8
8       0       9
9       0       -1

Enter ur choice:    4

Enter ur choice:    5
0       0       4
1       -1      -1
2       0       3
3       0       5
4       0       2
5       0       6
6       0       7
7       0       8
8       0       9
9       0       -1

Enter ur choice:    15
        ************WRONG ENTRY********

Enter ur choice:    7
Previous
Next Post »

Still not found what you are looking for? Try again here.