Вы находитесь на странице: 1из 4

Ex.No.

9 CURSOR IMPLEMENTATION – LIST

Algorithm:

1. Start the program.


2. Create a node with two fields data and link field.
 Allocate space for the node dynamically.
 Create link between the created nodes and let the last node be with NULL Link
 Insert the input data in the data field and press –1 to stop the same.
3. Get the choice of operations either insertion or deletion.
4. For insertion get the position in which insertion is to be done and the element to be
inserted. Check for the start, middle or end position of insertion. Insert the node and
change its link accordingly.
5. For deletion get the position in which deletion is to be done. Delete the node and then
link it to the next node. Before deletion check whether there is data in the list to be
deleted.
6. Using display option list the elements of the list.
7. Stop the program.

PROGRAM

#include<stdio.h>
#include<conio.h>
struct cursor
{
char data;
int next;
}c[10];
int fp=0;
void create()
{
char a;
int p,i;
for(i=0;i<10;i++)
{
c[i].data=' ';
c[i].next=-1;
}
printf("Enter the first element to be inserted");
scanf("%c",&a);
printf("Enter the position");
scanf("%d",&p);
fp=p;
c[p].data=a;
c[p].next=0;
}
void insert(char a,int pos)
{
int i;
if(c[pos].next==-1)
{
c[pos].data=a;
for(i=0;i<10;i++)
{
if(c[i].next==0)
{
c[i].next=pos;
}
}
c[pos].next=0;
}
else
printf("\n Already data is available");
}

void display()
{
int i;
i=fp;
do
{
printf("%d\t",i);
printf("%c\t",c[i].data);
printf("%d\n",c[i].next);
i=c[i].next;
}
while(i>0);
}
void del()
{
int temp,i;
char d;
printf("\n Enter the character to be deleted");
d=getch();
if(c[fp].data==d)
{
c[fp].data=' ';
temp=c[fp].next;
c[fp].next=-1;
fp=temp;
}
else
{
i=fp;
do
{
if(c[c[i].next].data==d)
{
temp=c[i].next;
c[i].next=c[c[i].next].next;
c[temp].next=-1;
}
i=c[i].next;
}
while(i>0);
}
}

void main()
{
int opt,p;
char a;
clrscr();
create();
do
{
printf("\n 1.Insert");
printf("\n 2.Delete");
printf("\n 3.Display");
printf("\n 4.Exit");
printf("\n Enter your choice");
scanf("%d",&opt);
switch(opt)
{
case 1:
printf("\nEnter the element to be inserted");
a=getch();
printf("\nEnter the position");
scanf("%d",&p);
insert(a,p);
break;

case 2:
del();
break;

case 3:
display();
break;
}
}
while(opt<4);
}

Вам также может понравиться