List-Array Implementation: CS6202-Programming & Data Structures
List-Array Implementation: CS6202-Programming & Data Structures
List-Array Implementation: CS6202-Programming & Data Structures
LIST-ARRAY IMPLEMENTATION
#include<stdio.h>
#define MAX 10
int a[10];
int i,ch,n;
void insert()
{
int i,num,pos;
printf("enter the number to insert\n");
scanf("%d", &num);
printf("enter the position in which it has to be inserted\n");
scanf("%d", &pos);
for(i=n;i>=pos;i--)
{
a[i]=a[i-1];
}
a[pos]=num;
n++;
}
void insfirst()
{
int i,num;
printf("enter the number to insert\n");
scanf("%d", &num);
for(i=n;i>=0;i--)
{
a[i]=a[i-1];
}
a[0]=num;
n++;
}
void inslast()
{
int num;
printf("enter the number to insert\n");
scanf("%d",&num);
a[n]=num;
n++;
}
void display()
{
1
CS6202- Programming & Data structures
int i;
printf("list elemts\n");
for(i=0;i<n;i++)
{
printf("%d\t",a[i]);
}
}
void search()
{
int i,num;
printf("enter the element to be searched\n");
scanf("%d",&num);
for(i=0;i<n;i++)
{
if(a[i]==num)
{
printf("\n The element %d is present at the %dth position",num,i+1);
return;
}
}
}
void del()
{
int pos,i;
printf("enter the position of elmt to be deleted\n");
scanf("%d",&pos);
for(i=pos;i<n;i++)
{
a[i-1]=a[i];
}
n--;
}
void main()
{
printf("------creating a list------\n\n" );
printf("Enter the number of elements in the list\n");
scanf("%d",&n);
if(n>MAX)
{
printf("out of range\n");
}
2
CS6202- Programming & Data structures
3
CS6202- Programming & Data structures
OUTPUT:
------creating a list------
Enter the number of elements in the list
3
enter the elements
1
2
3
elements in the list
1 2 3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
1
enter the number to insert
4
enter the position in which it has to be inserted
2
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
1 2 4 3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
4
4
CS6202- Programming & Data structures
5
CS6202- Programming & Data structures
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
3
enter the position of elmt to be deleted
3
1.Insert
2.Display
3.Delete
4.search
5.Insertfirst
6.InsertLast
7.Exit
2
list elemts
8 1 4 3 5
6
CS6202- Programming & Data structures
int islast(position p)
{
return p->next==NULL;
}
list getnode()
{
position p;
p=(list)malloc(sizeof(struct node));
return p;
}
void display(list l)
{
printf("The list elements are:\n");
while(l->next!=NULL)
{
printf("%d->",l->element);
l=l->next;
}
printf("%d->",l->element);
7
CS6202- Programming & Data structures
case 2:
printf("\nEnter the key after which it has to be inserted: ");
scanf("%d",&key);
p=find(key,new);
tmpcell->element=x;
tmpcell->next=p->next;
p->next=tmpcell;
break;
case 3:
while(new->next!=NULL)
{
new=new->next;
}
tmpcell->element=x;
tmpcell->next=NULL;
new->next=tmpcell;
8
CS6202- Programming & Data structures
break;
}
}
void create ( )
{
head=getnode();
printf("\nEnter the header element:");
scanf("%d",&head->element);
head->next=NULL;
}
void deletion(list l)
{
int x,ch;
position p,tmpcell;
if(l->next==NULL)
printf("LIST IS EMPTY\n");
else
{
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(l->next->next==NULL)
{
l->next=NULL;
free(l->next);
}
else
{
tmpcell=l->next;
9
CS6202- Programming & Data structures
l->next=l->next->next;
free(tmpcell);
}
break;
case 2:
printf("\nEnter the elemnt to be deleted:");
scanf("%d",&x);
p=findprevious(x,l);
if(!islast(p))
{
tmpcell=p->next;
p->next=tmpcell->next;
free(tmpcell);
}
break;
case 3:
p=l;
while(p->next!=NULL)
{
tmpcell=p;
p=p->next;
}
tmpcell->next=NULL;
free(p);
break;
}
}
}
void main()
{
list p;
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
10
CS6202- Programming & Data structures
break;
case 2:
display(head);
break;
case 3:
printf("\nEnter the element:");
scanf("%d",&ele);
insert(ele,head);
break;
case 4:
deletion(head);
break;
case 5:
exit(0);
}
}
}
OUTPUT:
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:1
Enter the header element:0
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:2
1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display
11
CS6202- Programming & Data structures
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:5
1.first
2.middle
3.last
Enter the choice:
3
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->2->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:4
1.first
2.middle
3.last
Enter the choice:
2
Enter the key after which it has to be inserted: 2
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:8
1.first
2.middle
3.last
Enter the choice:
1
1.Create
12
CS6202- Programming & Data structures
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:9
1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->9->8->2->4->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.first
2.middle
3.last
Enter the choice:
1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
The list elements are:
0->8->2->4->5->
1.Create
2.Display
3.Insert
4.Delete
5.Exit
13
CS6202- Programming & Data structures
14
CS6202- Programming & Data structures
struct node
{
int ele;
position next;
position prev;
};
position create();
position find(int,list);
void insert(int,list);
void display(list);
void deletion(list);
void main()
{
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
head=create();
break;
case 2:
display(head);
break;
case 3:
printf("\nEnter the element:");
scanf("%d",&ele);
insert(ele,head);
break;
15
CS6202- Programming & Data structures
case 4:
deletion(head);
break;
case 5:
exit(0);
}
}
}
case 2:
printf("\nEnter the element to be deleted:");
scanf("%d",&x);
p=find(x,temp);
p->prev->next=p->next;
p->next->prev=p->prev;
free(p);
16
CS6202- Programming & Data structures
break;
case 3:
while(r->next!=NULL)
{
r=r->next;
}
temp=r->prev;
temp->next=NULL;
free(r);
break;
}
}
position create()
{
head=(list)malloc(sizeof(struct node));
if(head==NULL)
{
printf("\nunable 2 create head node");
}
printf("\nEnter the header node value:");
scanf("%d",&head->ele);
head->prev=head->next=NULL;
return head;
}
17
CS6202- Programming & Data structures
case 2:
printf("\nEnter the elment after which it has to be inserted: ");
scanf("%d",&key);
temp=(list)malloc(sizeof(struct node));
p=find(key,head);
temp->ele=x;
temp->prev=p;
temp->next=p->next;
temp->next->prev=temp;
p->next=temp;
break;
case 3:
temp=(list)malloc(sizeof(struct node));
temp->ele=x;
while(r->next!=NULL)
{
r=r->next;
}
temp->prev=r;
temp->next=NULL;
r->next=temp;
break;
}
}
18
CS6202- Programming & Data structures
OUTPUT:
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:1
Enter the header node value:0
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:1
1.Beggining
2.Middle
3.End
Enter the choice: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:4
1.Beggining
2.Middle
3.End
Enter the choice: 3
19
CS6202- Programming & Data structures
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 1 4
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:6
1.Beggining
2.Middle
3.End
Enter the choice: 2
Enter the elment after which it has to be inserted: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:8
1.Beggining
2.Middle
3.End
Enter the choice: 1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:3
Enter the element:9
1.Beggining
2.Middle
3.End
Enter the choice: 3
1.Create
2.Display
3.Insert
20
CS6202- Programming & Data structures
4.Delete
5.Exit
Enter the choice:2
0 8 1 6 4 9
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 2
Enter the element to be deleted:1
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 8 6 4 9
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 1
deleted element is 8
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:4
1.Start
2.Intermediate
3.End
Enter the choice: 3
21
CS6202- Programming & Data structures
1.Create
2.Display
3.Insert
4.Delete
5.Exit
Enter the choice:2
0 6 4
22
CS6202- Programming & Data structures
list getnode()
{
position p;
p=(list)malloc(sizeof(struct node));
return p;
}
void display(list l)
{
position temp;
printf("The list elements are:\n");
if(l->next==head)
{
printf("List is empty\n");
return;
}
temp=head->next;
while(temp!=head)
{
printf("%d->",temp->element);
temp=temp->next;
}
}
23
CS6202- Programming & Data structures
void insert(int x)
{
position cur,tmpcell;
int ch,key;
tmpcell=getnode();
if(tmpcell==NULL)
{
printf("out of space\n");
}
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
tmpcell->element=x;
tmpcell->next=head->next;
head->next=tmpcell;
break;
case 2:
printf("\nEnter the key after which it has to be inserted: ");
scanf("%d",&key);
cur=find(key,head);
tmpcell->element=x;
tmpcell->next=cur->next;
cur->next=tmpcell;
break;
case 3:
cur=head->next;
while(cur->next!=head) //changes
{
cur=cur->next;
}
tmpcell->element=x;
tmpcell->next=head; //changes
cur->next=tmpcell;
break;
}
}
24
CS6202- Programming & Data structures
void create ( )
{
head=getnode();
printf("\nEnter the header element:");
scanf("%d",&head->element);
head->next=head; ///changes
}
void deletion(list l)
{
int x,ch;
position p,tmpcell;
if(l->next==NULL)
printf("LIST IS EMPTY\n");
else
{
printf("\n1.first\n2.middle\n3.last\n");
printf("Enter the choice:\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
if(l->next->next==head) //changes
{
l->next=head; //changes
free(l->next);
}
else
{
tmpcell=l->next;
l->next=l->next->next;
free(tmpcell);
}
break;
case 2:
printf("\nEnter the elemnt to be deleted:");
scanf("%d",&x);
position prev;
p=l;
while(p->element!=x) //changes
{
prev=p;
p=p->next;
25
CS6202- Programming & Data structures
}
tmpcell=prev->next;
prev->next=tmpcell->next;
free(tmpcell);
break;
case 3:
p=l;
while(p->next!=head) //changes
{
tmpcell=p;
p=p->next;
}
//tmpcell=findprevious(p->element,l);
tmpcell->next=head;
free(p);
break;
}
}
}
void main()
{
list p;
int choice,ele,key;
while(1)
{
printf("\n1.Create\n2.Display\n3.Insert\n4.Delete\n5.Exit");
printf("\nEnter the choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
create();
break;
case 2:
display(head);
break;
case 3:
printf("\nEnter the element:");
scanf("%d",&ele);
26
CS6202- Programming & Data structures
insert(ele);
break;
case 4:
deletion(head);
break;
case 5:
exit(0);
}
}
}
27
CS6202- Programming & Data structures
#include<stdio.h>
#include<math.h>
/*
This structure is used to store a polynomial term. An array of such terms represents a
polynomial.
*/
struct poly
float coeff;
int exp;
};
//declaration of polynomials
int main()
int i;
int k=0,l=0,m=0;
28
CS6202- Programming & Data structures
scanf("%d",°1);
for(i=0;i<=deg1;i++)
scanf("%f",&a[i].coeff);
a[k++].exp = i;
scanf("%d",°2);
for(i=0;i<=deg2;i++)
scanf("%f",&b[i].coeff);
29
CS6202- Programming & Data structures
b[l++].exp = i;
printf("\nExpression 1 = %.1f",a[0].coeff);
for(i=1;i<=deg1;i++)
printf("+ %.1fx^%d",a[i].coeff,a[i].exp);
printf("\nExpression 2 = %.1f",b[0].coeff);
for(i=1;i<=deg2;i++)
printf("+ %.1fx^%d",b[i].coeff,b[i].exp);
if(deg1>deg2)
for(i=0;i<=deg2;i++)
30
CS6202- Programming & Data structures
c[m].exp = a[i].exp;
m++;
for(i=deg2+1;i<=deg1;i++)
c[m].coeff = a[i].coeff;
c[m].exp = a[i].exp;
m++;
else
for(i=0;i<=deg1;i++)
c[m].exp = a[i].exp;
m++;
for(i=deg1+1;i<=deg2;i++)
c[m].coeff = b[i].coeff;
c[m].exp = b[i].exp;
m++;
31
CS6202- Programming & Data structures
for(i=1;i<m;i++)
printf("+ %.1fx^%d",c[i].coeff,c[i].exp);
return 0;
32
CS6202- Programming & Data structures
void main( )
{
// POLY *start1=NULL,*start2=NULL;
printf("Polynomial 1 is : ");
display(start1);
printf("Polynomial 2 is : ");
display(start2);
poly_add(start1, start2);
poly_mult(start1, start2);
}/*End of main()*/
33
CS6202- Programming & Data structures
34
CS6202- Programming & Data structures
else
printf("\n");
}
}/*End of display()*/
void poly_add(POLY p1,POLY p2)
{
POLY start3;
start3=NULL;
35
CS6202- Programming & Data structures
OUTPUT:
Enter polynomial 1 :
Enter the number of terms : 2
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 1
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 0
Enter polynomial 2 :
Enter the number of terms : 2
Enter coeficient for term 1 : 3
Enter exponent for term 1 : 1
Enter coeficient for term 2 : 4
Enter exponent for term 2 : 0
Polynomial 1 is : (3.0x^1) + (4.0x^0)
Polynomial 2 is : (3.0x^1) + (4.0x^0)
Added polynomial is : (6.0x^1) + (8.0x^0)
Multiplied polynomial is : (9.0x^2) + (12.0x^1) + (12.0x^1) + (16.0x^0)
36