/*Array Implementation of list */
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 30
typedef struct list
{ int data[MAX];
int n;
}list;
void insert( list *p,int position,int x);
void Delete(list *p,int position);
int search(list *p,int x);
void print(list *p);
void read(list *p);
void init(list *l);
void main()
{ list l;
int x,op,position;
clrscr();
do{
flushall();
printf("\n1)create\n2)insert\n3)delete\n4)search\n5)print\n6)quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: read(&l);
break;
case 2: printf("\n enter the position and the data to be inserted : ");
scanf("%d%d",&position,&x);
insert(&l,position,x);
break;
case 3:printf("\n enter the position : ");
scanf("%d",&position);
Delete(&l,position);
break;
case 4: printf("\nenter the number to be searched:");
scanf("%d",&x);
position=search(&l,x);
if(position==-1)
printf("\nnot found");
else
printf("Found at the position : %d",position+1);
break;
case 5: print(&l);
break;
default:break;
}
}while(op!=6);
}
void insert( list *p,int position,int x)
{ int i;
if(position<1 || position > p->n+1)
{ printf("\nIncorrect postion :");
return;
}
for(i=p->n-1;i>=position-1;i--) /*index is 1 less than position*/
p->data[i+1]=p->data[i];
p->data[position-1]=x;
p->n=p->n+1;
}
void Delete(list *p,int position)
{ int i;
if(position<1 || position >p->n)
{ printf("\Incorrect Position ");
return;
}
for(i=position;i<p->n;i++)
p->data[i-1]=p->data[i];
p->n=p->n-1;
}
int search(list *p,int x)
{ int i;
for(i=0;i<p->n;i++)
if(x==p->data[i])
return(i);
return(-1);
}
void print(list *p)
{ int i;
printf("\n");
for(i=0;i<p->n;i++)
printf("%d ",p->data[i]);
}
void read(list *p)
{ int i,n;
printf("\nEnter No. of data : ");
scanf("%d",&n);
p->n=n;
printf("\Enter data : ");
for(i=0;i<n;i++)
scanf("%d", &(p->data[i]));
}
OUTPUT:-
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:1
Enter No. of data : 4
Enter data : 78 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:2
enter the position and the data to be inserted : 2 5
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 5 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3
enter the position : 3
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3
enter the position : 6
Incorrect Position
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 5 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4
enter the number to be searched:9
not found
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4
enter the number to be searched:5
Found at the position : 2
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:6
#include <stdio.h>
#include <conio.h>
#include <string.h>
#define MAX 30
typedef struct list
{ int data[MAX];
int n;
}list;
void insert( list *p,int position,int x);
void Delete(list *p,int position);
int search(list *p,int x);
void print(list *p);
void read(list *p);
void init(list *l);
void main()
{ list l;
int x,op,position;
clrscr();
do{
flushall();
printf("\n1)create\n2)insert\n3)delete\n4)search\n5)print\n6)quit");
printf("\nEnter Your Choice:");
scanf("%d",&op);
switch(op)
{ case 1: read(&l);
break;
case 2: printf("\n enter the position and the data to be inserted : ");
scanf("%d%d",&position,&x);
insert(&l,position,x);
break;
case 3:printf("\n enter the position : ");
scanf("%d",&position);
Delete(&l,position);
break;
case 4: printf("\nenter the number to be searched:");
scanf("%d",&x);
position=search(&l,x);
if(position==-1)
printf("\nnot found");
else
printf("Found at the position : %d",position+1);
break;
case 5: print(&l);
break;
default:break;
}
}while(op!=6);
}
void insert( list *p,int position,int x)
{ int i;
if(position<1 || position > p->n+1)
{ printf("\nIncorrect postion :");
return;
}
for(i=p->n-1;i>=position-1;i--) /*index is 1 less than position*/
p->data[i+1]=p->data[i];
p->data[position-1]=x;
p->n=p->n+1;
}
void Delete(list *p,int position)
{ int i;
if(position<1 || position >p->n)
{ printf("\Incorrect Position ");
return;
}
for(i=position;i<p->n;i++)
p->data[i-1]=p->data[i];
p->n=p->n-1;
}
int search(list *p,int x)
{ int i;
for(i=0;i<p->n;i++)
if(x==p->data[i])
return(i);
return(-1);
}
void print(list *p)
{ int i;
printf("\n");
for(i=0;i<p->n;i++)
printf("%d ",p->data[i]);
}
void read(list *p)
{ int i,n;
printf("\nEnter No. of data : ");
scanf("%d",&n);
p->n=n;
printf("\Enter data : ");
for(i=0;i<n;i++)
scanf("%d", &(p->data[i]));
}
OUTPUT:-
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:1
Enter No. of data : 4
Enter data : 78 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:2
enter the position and the data to be inserted : 2 5
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 5 45 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3
enter the position : 3
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:3
enter the position : 6
Incorrect Position
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:5
78 5 56 90
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4
enter the number to be searched:9
not found
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:4
enter the number to be searched:5
Found at the position : 2
1)create
2)insert
3)delete
4)search
5)print
6)quit
Enter Your Choice:6
Wonderful collection of the posts!! These will be definitely helpful for everyone.
ReplyDeletetutorial on c++