Sunday, January 26, 2014

C Program to Demonstrate Array Implementation of List

with 0 Comment
C Program to Demonstrate Array Implementation of List:

Main Program:

#include<stdio.h>
#include<conio.h>
#include "c:\tc\bin\subail.c"
void main()
{
int no,pos,s,c;
clrscr();
while(1)
{
printf("\n1.Append\n2.Printlist\n3.Insert\n4.Delete\n5.Search\n6.make Empty\n7.Exit\n");
scanf("%d",&c);
switch(c)
{
case 1:
printf("Enter the element to be append");
scanf("%d",&no);
append(no);
break;
case 2:
printlist();
break;
case 3:
printf("Enter the elment & Position of insertion\n");
scanf("%d%d",&no,&pos);
insert(no,pos);
break;
case 4:
printf("Enter the element to be deleted");
scanf("%d",&no);
delete(no);
break;
case 5:
printf("Enter the element to be found");
scanf("%d",&no);
s=find(no);
if(s==-1)
printf("Element is not Found");
else
printf("Element is at %d\n",s);
break;
case 6:
makeempty();
break;
case 7:exit(0);
default :printf("Enter a correct choice\n");
}
}
}

SubProgram: (Saved as 'subail.c' ) in the same folder of Main Program

#define size 10
int a[size];
int curpos=0;
void printlist()
{
int i;
if(curpos==0)
printf("No elements in array");
else
for(i=0;i<curpos;i++)
printf("Element in %d the position is %d\n",i,a[i]);
}
void append(int elem)
{
if(curpos<size)
{
a[curpos]=elem;
curpos++;
}
else
printf("List is full");
}
void insert(int elem,int pos)
{
int i;
if(curpos==size)
printf("Can`t do insertion");
else
{
for(i=curpos;i!=pos;i--)
a[i]=a[i-1];
a[pos]=elem;
curpos++;
}
}
int find(int elem)
{
int i,j=-1;
for(i=0;i<curpos;i++)
if(a[i]==elem)
return i;
if(i==curpos)
return j;
}
void delete(int elem)
{
int j;
int temp;
temp=find(elem);
if(temp==-1)
printf("Element %d is not found",temp);
else
{
for(j=temp;j<curpos;j++)
a[j]=a[j+1];
curpos--;
}
}
void makeempty()
{
int i;
for(i=0;i<curpos;i++)
a[i]=0;
curpos=0;
}

0 comments:

Post a Comment

Powered by Blogger.

Blog Archive