SOURCE CODE:-
#include<stdio.h>
#define MAX 10
struct cursor
{
int data;
int next;
}cursor[MAX];
void initcursor()
{
int i;
for(i=0;i<MAX-1;i++)
cursor[i].next=i+1;
cursor[MAX-1].next=0;
}
int cualloc()
{
int pos;
if(cursor[0].next==0)
{
printf("List is full!**!");
return -1;
}
pos=cursor[0].next;
cursor[0].next=cursor[pos].next;
return pos;
}
void display(int p)
{
int q=p;
if(cursor[q].next==0)
printf("Fatal Error:List is empty!**!\n");
else
{
printf("\nThe contents of the list are:\n");
while(cursor[q].next!=0)
{
q=cursor[q].next;
printf("%d\t",cursor[q].data);
}
}
}
void displayfree(int p)
{
int q=p;
if(cursor[q].next==0)
printf("Fatal Error:Free List is empty!**!\n");
else
{
printf("The contents of the free list are:\n");
q=cursor[q].next;
while(q!=0)
{
printf("%d\t",q);
q=cursor[q].next;
}
}
}
int count(int p)
{
int q=p,c=0;
while(cursor[q].next!=0)
{
c++;
q=cursor[q].next;
}
return c;
}
void insertatbeg(int p,int num)
{
int q=p,pos=cualloc();
if(pos!=-1)
{
cursor[pos].data=num;
cursor[pos].next=cursor[q].next;
cursor[q].next=pos;
}
display(p);
}
void insertatmid(int p,int pos,int num)
{
int q=p,i;
int fpos=cualloc();
if(pos>count(p))
{
printf("Fatal Error:No such position found in list!**!");
return;
}
for(i=1;i<pos;i++)
q=cursor[q].next;
cursor[fpos].data=num;
cursor[fpos].next=cursor[q].next;
cursor[q].next=fpos;
display(p);
}
void insertatend(int p,int num)
{
int pos=cualloc(),q=p;
if(pos!=-1)
{
cursor[pos].data=num;
while(cursor[q].next!=0)
q=cursor[q].next;
cursor[q].next=pos;
cursor[pos].next=0;
display(p);
}
}
int locate(int p,int num)
{
int q=p,i=0;
while(cursor[q].next!=0)
{
if(cursor[q].data==num)
return i;
q=cursor[q].next;
++i;
}
printf("Fatal Error:Element %d not found!**!",num);
return -1;
}
void retrieve(int p,int pos)
{
int q=p,i;
if(pos<=count(p))
{
for(i=1;i<=pos;i++)
q=cursor[q].next;
printf("Element found at position %d is %d",pos,cursor[q].data);
return;
}
else
printf("Fatal Error:No such position found in list!**!");
}
int max(int p)
{
int max,q=p;
max=cursor[cursor[q].next].data;
while(cursor[q].next!=0)
{
if(max<cursor[cursor[q].next].data)
max=cursor[cursor[q].next].data;
q=cursor[q].next;
}
return max;
}
int min(int p)
{
int q=p,min;
min=cursor[cursor[q].next].data;
while(cursor[q].next != 0)
{
if (min>cursor[cursor[q].next].data)
min=cursor[cursor[q].next].data;
q=cursor[q].next;
}
return min;
}
void deleteno(int p,int num)
{
int q=p,pos=locate(p,num),i=1,temp;
if(pos==-1)
return;
else
{
if(pos!=1)
{
for(i=1;i<pos;i++)
q=cursor[q].next;
}
temp=cursor[q].next;
cursor[q].next=cursor[temp].next;
cursor[temp].next=cursor[0].next;
cursor[0].next=temp;
}
display(p);
}
void deletepos(int p,int pos)
{
int q=p,i=1,temp;
if(pos<=count(p))
{
while(i<pos)
{
q=cursor[q].next;
i++;
}
temp=cursor[q].next;
cursor[q].next=cursor[temp].next;
cursor[temp].next=cursor[0].next;
cursor[0].next=temp;
display(p);
}
else
printf("Fatal Error:No such position in list!**!\n");
}
void sort(int p)
{
int i=1,j=1,q=p,temp;
if(cursor[q].next == 0)
printf("Fatal Error:List is empty!**!");
else
{
while(i<=count(p))
{
j=1;
q=cursor[p].next;
while(j<count(p))
{
if(cursor[q].data>cursor[cursor[q].next].data)
{
temp=cursor[q].data;
cursor[q].data=cursor[cursor[q].next].data;
cursor[cursor[q].next].data=temp;
}
q=cursor[q].next;
j++;
}
i++;
}
}
display(p);
}
int main()
{
int ch,ch1,num,pos,p;
initcursor();
p=cualloc();
cursor[0].next=cursor[p].next;
cursor[p].next=0;
while(1)
{
printf("\nMENU:\n\t1.Insert\n\t2.Display\n\t3.Count\n\t4.Delete\n\t5.Search\n\t6.Display free list\n\t7.Sort\n\t8.Maximum\n\t9.Minimum\n\t10.Exit\nEnter your choice:");
scanf("%d",&ch);
if(ch==10)
break;
switch(ch)
{
case 1:
printf("INSERT MENU:\n\t1.At Beginning\n\t2.At Middle\n\t3.At End\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be inserted at beginning:");
scanf("%d",&num);
insertatbeg(p,num);
break;
case 2:
printf("Enter the position at which number is to be inserted:");
scanf("%d",&pos);
printf("Enter the number to be inserted at position %d: ",pos);
scanf("%d",&num);
insertatmid(p,pos,num);
break;
case 3:
printf("Enter the element to be inserted at last:");
scanf("%d",&num);
insertatend(p,num);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 2:display(p);break;
case 3:printf("Count: %d",count(p));break;
case 4:
printf("DELETE MENU:\n\t1.By data\n\t2.By position\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be deleted:");
scanf("%d",&num);
deleteno(p,num);
break;
case 2:
printf("Enter the position to be deleted:");
scanf("%d",&pos);
deletepos(p,pos);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 5:
printf("SEARCH MENU:\n\t1.Locate\n\t2.Retrieve\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be located:");
scanf("%d",&num);
pos=locate(p,num);
if(pos!=0)
printf("Element %d is located at position %d",num,pos);
break;
case 2:
printf("Enter the position:");
scanf("%d",&pos);
retrieve(p,pos);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 6:displayfree(0);break;
case 7:sort(p);break;
case 8:printf("Maximum data:%d",max(p));break;
case 9:printf("Minimum data:%d",min(p));break;
default:printf("Enter correct menu option!**!");break;
}
}
return 0;
}
#include<stdio.h>
#define MAX 10
struct cursor
{
int data;
int next;
}cursor[MAX];
void initcursor()
{
int i;
for(i=0;i<MAX-1;i++)
cursor[i].next=i+1;
cursor[MAX-1].next=0;
}
int cualloc()
{
int pos;
if(cursor[0].next==0)
{
printf("List is full!**!");
return -1;
}
pos=cursor[0].next;
cursor[0].next=cursor[pos].next;
return pos;
}
void display(int p)
{
int q=p;
if(cursor[q].next==0)
printf("Fatal Error:List is empty!**!\n");
else
{
printf("\nThe contents of the list are:\n");
while(cursor[q].next!=0)
{
q=cursor[q].next;
printf("%d\t",cursor[q].data);
}
}
}
void displayfree(int p)
{
int q=p;
if(cursor[q].next==0)
printf("Fatal Error:Free List is empty!**!\n");
else
{
printf("The contents of the free list are:\n");
q=cursor[q].next;
while(q!=0)
{
printf("%d\t",q);
q=cursor[q].next;
}
}
}
int count(int p)
{
int q=p,c=0;
while(cursor[q].next!=0)
{
c++;
q=cursor[q].next;
}
return c;
}
void insertatbeg(int p,int num)
{
int q=p,pos=cualloc();
if(pos!=-1)
{
cursor[pos].data=num;
cursor[pos].next=cursor[q].next;
cursor[q].next=pos;
}
display(p);
}
void insertatmid(int p,int pos,int num)
{
int q=p,i;
int fpos=cualloc();
if(pos>count(p))
{
printf("Fatal Error:No such position found in list!**!");
return;
}
for(i=1;i<pos;i++)
q=cursor[q].next;
cursor[fpos].data=num;
cursor[fpos].next=cursor[q].next;
cursor[q].next=fpos;
display(p);
}
void insertatend(int p,int num)
{
int pos=cualloc(),q=p;
if(pos!=-1)
{
cursor[pos].data=num;
while(cursor[q].next!=0)
q=cursor[q].next;
cursor[q].next=pos;
cursor[pos].next=0;
display(p);
}
}
int locate(int p,int num)
{
int q=p,i=0;
while(cursor[q].next!=0)
{
if(cursor[q].data==num)
return i;
q=cursor[q].next;
++i;
}
printf("Fatal Error:Element %d not found!**!",num);
return -1;
}
void retrieve(int p,int pos)
{
int q=p,i;
if(pos<=count(p))
{
for(i=1;i<=pos;i++)
q=cursor[q].next;
printf("Element found at position %d is %d",pos,cursor[q].data);
return;
}
else
printf("Fatal Error:No such position found in list!**!");
}
int max(int p)
{
int max,q=p;
max=cursor[cursor[q].next].data;
while(cursor[q].next!=0)
{
if(max<cursor[cursor[q].next].data)
max=cursor[cursor[q].next].data;
q=cursor[q].next;
}
return max;
}
int min(int p)
{
int q=p,min;
min=cursor[cursor[q].next].data;
while(cursor[q].next != 0)
{
if (min>cursor[cursor[q].next].data)
min=cursor[cursor[q].next].data;
q=cursor[q].next;
}
return min;
}
void deleteno(int p,int num)
{
int q=p,pos=locate(p,num),i=1,temp;
if(pos==-1)
return;
else
{
if(pos!=1)
{
for(i=1;i<pos;i++)
q=cursor[q].next;
}
temp=cursor[q].next;
cursor[q].next=cursor[temp].next;
cursor[temp].next=cursor[0].next;
cursor[0].next=temp;
}
display(p);
}
void deletepos(int p,int pos)
{
int q=p,i=1,temp;
if(pos<=count(p))
{
while(i<pos)
{
q=cursor[q].next;
i++;
}
temp=cursor[q].next;
cursor[q].next=cursor[temp].next;
cursor[temp].next=cursor[0].next;
cursor[0].next=temp;
display(p);
}
else
printf("Fatal Error:No such position in list!**!\n");
}
void sort(int p)
{
int i=1,j=1,q=p,temp;
if(cursor[q].next == 0)
printf("Fatal Error:List is empty!**!");
else
{
while(i<=count(p))
{
j=1;
q=cursor[p].next;
while(j<count(p))
{
if(cursor[q].data>cursor[cursor[q].next].data)
{
temp=cursor[q].data;
cursor[q].data=cursor[cursor[q].next].data;
cursor[cursor[q].next].data=temp;
}
q=cursor[q].next;
j++;
}
i++;
}
}
display(p);
}
int main()
{
int ch,ch1,num,pos,p;
initcursor();
p=cualloc();
cursor[0].next=cursor[p].next;
cursor[p].next=0;
while(1)
{
printf("\nMENU:\n\t1.Insert\n\t2.Display\n\t3.Count\n\t4.Delete\n\t5.Search\n\t6.Display free list\n\t7.Sort\n\t8.Maximum\n\t9.Minimum\n\t10.Exit\nEnter your choice:");
scanf("%d",&ch);
if(ch==10)
break;
switch(ch)
{
case 1:
printf("INSERT MENU:\n\t1.At Beginning\n\t2.At Middle\n\t3.At End\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be inserted at beginning:");
scanf("%d",&num);
insertatbeg(p,num);
break;
case 2:
printf("Enter the position at which number is to be inserted:");
scanf("%d",&pos);
printf("Enter the number to be inserted at position %d: ",pos);
scanf("%d",&num);
insertatmid(p,pos,num);
break;
case 3:
printf("Enter the element to be inserted at last:");
scanf("%d",&num);
insertatend(p,num);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 2:display(p);break;
case 3:printf("Count: %d",count(p));break;
case 4:
printf("DELETE MENU:\n\t1.By data\n\t2.By position\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be deleted:");
scanf("%d",&num);
deleteno(p,num);
break;
case 2:
printf("Enter the position to be deleted:");
scanf("%d",&pos);
deletepos(p,pos);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 5:
printf("SEARCH MENU:\n\t1.Locate\n\t2.Retrieve\nChoice:");
scanf("%d",&ch1);
switch(ch1)
{
case 1:
printf("Enter the data to be located:");
scanf("%d",&num);
pos=locate(p,num);
if(pos!=0)
printf("Element %d is located at position %d",num,pos);
break;
case 2:
printf("Enter the position:");
scanf("%d",&pos);
retrieve(p,pos);
break;
default:printf("Enter correct menu option!**!");
}
break;
case 6:displayfree(0);break;
case 7:sort(p);break;
case 8:printf("Maximum data:%d",max(p));break;
case 9:printf("Minimum data:%d",min(p));break;
default:printf("Enter correct menu option!**!");break;
}
}
return 0;
}
No comments:
Post a Comment