When we first examined the array based and node based implementations.docx
1. When we first examined the array based and node based
implementations of the List ADT, there was one difference in
the interface: The array based version had a capacity parameter
in the CreateList function. This parameter was used to
determine the fixed maximum capacity of the List. In this
exercise, you will revise the pseudocode for the Array based
ADT to remove the fixed capacity limit. Only one of the List
operations should have a change to its header: Creating an
array-based List should now take zero parameters, and should
initially create an array based List with a capacity of 1 element.
All other operations should have unchanged headers In each of
the insert operations, instead of returning false when the array
is full, the algorithm should attempt to "grow" the capacity of
the list, and then insert. To reduce the workload of this
assignment, you only need to provide revised pseudocode for
insertTail, not the other insert operations. The other operations
would be modified in the same way. a new growList operation
will be needed to increase the capacity of the list. This
operation will be used by insertTail. If we were completely
revising the array based List ADT, we would also use this grow
operation in insertHead, and insertAfter. The grow operation
should: o should now take zero parameters, and should initally
create an array based ist with a capacity o o 1. 2. allocate a new
larger array copy the contents of the old array into the new
array
Solution
#include<stdio.h>
#include<alloc.h>
2. #include<conio.h>
struct list
{
int capacity;
int size;
int *array;
};
typedef struct list *ptrToNode;
typedef ptrToNode LIST;
typedef int POSITION;
int Isempty(LIST L)
{
return L->size==0;
}
void MakeEmpty(LIST L)
{
if(Isempty(L))
printf(" LIST is already Empty");
else
{
L->size=0;
printf(" Now List becomes Empty");
}
3. }
LIST Createlist(int max)
{
LIST L;
L=(struct list*)malloc(sizeof(struct list));
if(L==NULL)
printf(" Fatal Error");
else
{
L->capacity=max;
L->array=(int*)malloc(sizeof(int)*max);
if(L->array==NULL)
printf(" Fatal Error");
else
{
L->size=0;
printf(" List is Created successfully");
}
}
return L;
}
int Isfull(LIST L)
{
4. return L->size==L->capacity;
}
void Insert(int x,LIST L,POSITION P)
{
int i;
if(Isfull(L))
printf(" List is Full");
else
{
for(i=L->size-1;i>=P;i--)
L->array[i+1]=L->array[i];
L->size++;
L->array[P]=x;
}
}
POSITION Findprevious(int x,LIST L)
{
POSITION P;
P=-1;
while(P!=L->size&&L->array[P+1]!=x)
{
P++;
}
5. return P;
}
POSITION Find(int x,LIST L)
{
POSITION P;
P=0;
while(P!=L->size&&L->array[P]!=x)
{
P++;
}
return P;
}
void Delete(int x,LIST L)
{
int i;
POSITION P;
P=Find(x,L);
if(P==L->size)
printf(" Element not found in the list");
else
{
for(i=P;i<L->size;i++)
7. {
LIST L=NULL;
POSITION P;
int a,choice,ch,element;
clrscr();
printf(" 1.Create 2.Insert 3.Delete 4.Display
5.MakeEmpty 6.Find 7.IsEmpty 8.IsFull 9.Deletelist
10.Exit ");
A:
printf(" Enter Ur Option:t");
scanf("%d",&choice);
switch(choice)
{
case 1:
if(L==NULL)
L=Createlist(5);
else
printf(" List is already created");
break;
case 2:
if(L==NULL)
printf(" List is not yet created");
else
{
printf(" Enter the Element to insert:t");
8. scanf("%d",&element);
if(L->size==0)
Insert(element,L,0);
else
{
printf(" where u want to
insert?t1:Frontt2:Backt3:middlet::: ");
scanf("%d",&ch);
if(ch==1)
Insert(element,L,0);
else
if(ch==2)
Insert(element,L,L->size);
else
if(ch==3)
{
printf(" Where you want to insert:t");
scanf("%d",&a);
P=Find(a,L);
if(P<L->size)
Insert(element,L,P);
else
printf(" Element is not in the list");
}
9. else
printf(" Ur choice is not available");
}
}
break;
case 3:
if(L==NULL)
printf(" List is not yet created");
if(Isempty(L))
printf(" List is empty");
else
{
printf(" Enter the element to delete:t");
scanf("%d",&a);
Delete(a,L);
}
break;
case 4:
if(L==NULL)
printf(" List is not yet created");
else
if(Isempty(L))
printf(" List is empty");
else
{
10. printf(" Elements present in the list are:");
Display(L);
}
break;
case 5:
if(L==NULL)
printf(" List is not yet created ");
else
MakeEmpty(L);
break;
case 6:
if(L==NULL)
printf(" Not yet created");
else
if(Isempty(L))
printf(" List is empty");
else
{
printf(" which element is to find:t");
scanf("%d",&a);
P=Find(a,L);
printf(" Element is at %dt[0 to 4 means present]t[5
means not present]",P);
}
11. break;
case 7:
if(L==NULL)
printf(" Not yet created");
else
if(Isempty(L))
printf(" List is empty");
else
printf(" List is not empty");
break;
case 8:
if(L==NULL)
printf(" Not yet created");
else
if(Isfull(L))
printf(" List is FULL");
else
printf(" List is not FULL");
break;
case 9:
if(L==NULL)
printf(" Not yet created");
else
{
L=Deletelist(L);
12. printf(" List is Deleted");
}
break;
case 10:
exit (0);
break;
default:
printf(" *******WRONG ENTRY*******");
break;
}
goto A;
}