Vous êtes sur la page 1sur 55

/* ARRAY IMPLEMENTATION OF STACK ADT

*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
#include<graphics.h>
#define STACK_SIZE 5
void displaymenu();
int PUSH(int);
int POP(int *);
int size();
int peek(int *);
void view();
int top=-1,stack[STACK_SIZE];
int isfull();
int isempty();
void main()
{
int status,data,choice;
clrscr();
while(1)
{
displaymenu();
printf("enter your choice: ");
scanf("%d",&choice);
switch(choice)
{
case 1:
{
printf("\n enter the new data: ");
scanf("%d",&data);
fflush(stdin);
status=PUSH(data);
if(status==-1)
printf("stack overflow on the PUSH");
break;
}
case 2:
{
status=POP(&data);
if(status==-1)
printf("stack overflow ont the POP");
else
printf("The poped value is: %d",data);
break;
}
case 3:
{
status=peek(&data);
if(status==-1)
printf("\n the stack is empty");
else
printf("\n the top most value is: %d",data);
break;
}
case 4:
{
printf("\n The stack size is: %d",STACK_SIZE);
printf("\n The current elements in the stack:
%d",size());
break;
}
case 5:
{
view();
break;
}
default:
printf("\n End of Run of your program");
exit(0);
}
}
}
void displaymenu()
{
printf("\n 1.PUSH");
printf("\n 2.POP");
printf("\n 3.PEEK");
printf("\n 4.SIZE");
printf("\n 5.VIEW");
printf("\n 6.EXIT \n");
}
void view()
{
int i;
if(isempty())
{
printf("\n stack is empty");
return;
}
printf("\n Elements from the top of the stack:
\n\n\t\t");
for(i=top;i>0;i--)
{
printf("--i %d",stack[i]);
}
if(isfull())
printf("\n stack is full");
}
int size()
{
return top+1;
}
int peek(int *value)
{
if(isempty())
return -1;
else
*value=stack[top];
return 0;
}
int PUSH(int data)
{
if(isfull())
return -1;
else
stack[++top]=data;
return 0;
}
int isfull()
{
if(top==STACK_SIZE-1)
return 1;
else
return 0;
}
int POP(int *value)
{
if(isempty())
return-1;
else
*value=stack[top--];
return 0;
}
int isempty()
{
if(top==-1)
return 1;
else
return 0;
}
OUTPUT:

1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:1
Enter the new data siva
1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:2
The popped value is: -28705
1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:3
The top most value is: -28705
1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:4
The stack size is: 5
The current element in the stack:
1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:5
Elements from top of the stack:->-28705
1.push
2.pop
3.peek
4.size
5.view
6.exit
Enter your choice:6
/* ARRAY IMPLEMENTATION OF LIST ADT */

#include<stdio.h>
#include<conio.h>
#include<process.h>
#define MAXLEN 100
char data;
int p;
typedef struct
{
int len;
char element[MAXLEN];
}
olist;
olist init()
{
olist l;
l.len=0;
return l;
}
olist insert(olist l,char ch,int pos)
{
int i;
if((pos<0)||(pos>l.len))
{
fprintf(stderr,"insert:invalid index %d \n",pos);
return l;
}
if(l.len==MAXLEN)
{
fprintf(stderr,"insert:list is already full \n");
return l;
}
for(i=l.len;i>pos;--i)
l.element[i]=l.element[i-1];
l.element[pos]=ch;
++l.len;
return l;
}
olist deletes(olist l,int pos)
{
int i;
if((pos<0)||(pos>=l.len))
{
fprintf(stderr,"delete:invalid index %d \n",pos);
return l;
}
for(i=pos;i<l.len;i++)
l.element[i]=l.element[i+1];
--l.len;
return l;
}
int ispresent(olist l,char ch)
{
int i;
for(i=0;i<l.len;i++)
if(l.element[i]==ch)
return i;
return -1;
}
char getelement(olist l,int pos)
{
if((pos<0)||(pos>=l.len))
{
fprintf(stderr,"get element:invalid index %d
\n",pos);
return '\0';
}
return l.element[pos];
}
void print(olist l)
{
int i;
for(i=0;i<l.len;++i)
printf("%c",l.element[i]);
}
void input()
{
fflush(stdin);
printf("Enter character to insert:");
data=getchar();
printf("\nEnter position: ");
scanf("%d",&p);
}
void main()
{
int c;
olist l;
clrscr();
printf("1.Insert ** 2.Delete ** 3.element at position
** 4.Position of element ** 5.View list\n");
l=init();
while(1)
{
printf("\nenter your choice");
scanf("%d",&c);
switch(c)
{
case 1:
{
input(l,data,p);
printf("\n");
break;
}
case 5:
{
printf("\ncurrent list is: ");
print(l);
printf("\n");
break;
}
case 4:
{
char ch;
fflush(stdin);
printf("Enter character to get position: ");
ch=getchar();
printf("character present at %d",ispresent(l,ch));
break;
}
case 2:
{
int dp;
printf("Enter position of element to be delete: ");
scanf("%d",&dp);
l=deletes(l,dp);
printf("\n");
break;
}
case 3:
{
int gp;
printf("enter position to get a element: ");
scanf("%d",&gp);
printf("element of
position1:%c\n",getelement(l,gp));
break;
}
default:
{
printf("Invalid choice...end of your program");
exit(0);
}
}
}
}
OUTPUT:

1.insert** 2.delete** 3.element at position**


4.position of element** 5.view list

Enter your choice: 1


Enter character to insert: 2
Enter position : 2
Insert : list is already full

Enter your choice: 2


Enter position of element to delete: 1
Delete: Invalid index 1

Enter your choice:3


Enter position to get a element : 1
Getelement : invalid index
Element of position 1 :0

Enter your choice: 4


Enter character to set position : 2
Character present at –1
Enter your choice : 5
Current list is god
/* POINTER IMPLEMENTAION OF STACK ADT
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node*next;
}
stack;
stack*getdata();
void view();
void displaymenu();
int push(int value);
int pop(int*value);
void releasenode(stack*newnode);
stack*topstk=NULL;
void main()
{
int data,status,choice;
clrscr();
displaymenu();
while(1)
{
printf("\n enter the choice \n");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("enter the elements \n");
fflush(stdin);
scanf("%d",&data);
status=push(data);
if(status==-1)
printf("\n mem is not available \n");
break;
case 2:
status=pop(&data);
if(status==-1)
printf("\n stack underflow on pop \n");
else
printf("\n poped value is %d",data);
break;
case 3:
view();
break;
default:
printf("\n end of the program \n");
exit(0);
}
}}
void displaymenu()
{
printf("\n Representation of stack using linked list
\n");
printf("\n\t1.push\n\t2.pop\n\t3.view\n\t4.exit");
}
int push(int value)
{
extern stack*topstk;
stack*newptr;
newptr=getdata();
if(newptr==NULL)
return(-1);
newptr->data=value;
newptr->next=topstk;
topstk=newptr;
return(0);
}
int pop(int*value)
{
extern stack*topstk;
stack*temp;
if(topstk==NULL)
return(-1);
temp=topstk;
topstk=topstk->next;
*value=temp->data;
releasenode(temp);
return(0);
}
stack*getdata()
{
return(stack*)malloc(sizeof(stack));
}
void releasenode(stack*newnode)
{
free(newnode);
}
void view()
{
extern stack*topstk;
stack*top=topstk;
if(top==NULL)
{
printf("\n stack is empty");
return;
}
printf("\n the content of the stack is");
for(;top!=NULL;top=top->next)
printf("\n\t%d",top->data);
}
OUTPUT:

Representation of stack using linked list:

1.push
2.pop
3.view
4.exit

Enter your choice: 1


Enter the element
23

Enter your choice: 1


Enter the element
45

Enter your choice:3


The content of the stack is
23
45

Enter your choice:2


The popped value is 45

Enter your choice:3


The content of the stack is
23
Enter your choice:4
/* POINTER IMPLEMENTATION USING LIST
ADT */

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int data;
struct node*next;
};
struct node*start=NULL,*ptr;
int n,element,index,delnode;
void create();
void insert();
void deletion();
void disp();
void main()
{
int ch;
clrscr();
do
{
printf("\nLINKED LIST: ");
printf(" 1.create 2.insert 3.deletion 4.display
5.exit\n");
printf("\nEnter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
create();
break;
case 2:
insert();
break;
case 3:
deletion();
break;
case 4:
disp();
break;
case 5:
break;
}
}
while(ch!=5);
}
void create()
{
struct node*temp;
printf("\nEnter the number: ");
scanf("%d",&n);
if(start==NULL)
{
start=(struct node*)malloc(sizeof(struct node));
start->data=n;
start->next=NULL;
ptr=start;
}
else
{
temp=(struct node*)malloc(sizeof(struct node));
temp->data=n;
temp->next=NULL;
ptr->next=temp;
ptr=temp;
}
}
void insert()
{
struct node*temp,*x;
printf("\nenter the element to be inserted: ");
scanf("%d",&element);
printf("\nenter the index of the element: ");
scanf("%d",&index);
ptr=start;
while(ptr->data!=index)
{
temp=ptr;
ptr=ptr->next;
}
x=(struct node*)malloc(sizeof(struct node));
if(ptr!=start)
{
x->data=element;
x->next=ptr;
temp->next=x;
}
else
{
start=x;
start->data=element;
start->next=ptr;
}
}
void deletion()
{
struct node*pre;
printf("\nenter the node to be deleted: ");
scanf("%d",&delnode);
printf("\n%d is deleted from the list\n",delnode);
ptr=start;
while(ptr->data!=delnode)
{
pre=ptr;
ptr=ptr->next;
}
if(ptr!=start)
pre->next=ptr->next;
else
{
start=ptr->next;
free(ptr);
}
}
void disp()
{
ptr=start;
printf("\nThe element in the list: \n");
while(ptr!=NULL)
{
printf("%d\n",ptr->data);
ptr=ptr->next;
}
getch();
}
OUTPUT:

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 1
Enter the number: 2

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 1
Enter the number: 3

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 2
Enter the element to be inserted: 4
Enter the index of the element:2

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 4
The elements in the list are 4 2 3

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 3
Enter the node to be deleted: 2
2 is deleted from the list
LINKED LIST: 1.create 2.Insert 3.Deletion
4.Display 5.Exit
Enter your choice: 4
The elements in the list are 4 3

LINKED LIST: 1.create 2.Insert 3.Deletion


4.Display 5.Exit
Enter your choice: 5
QUEUE IMPLEMENTATION USING ARRAYS
*/

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int q[5],f=5,r=5;
void main()
{
int ch,n,i;
clrscr();

printf("1.enqueue\n2.dequeue\n3.display\n4.exit\n");
while(1)
{
printf("\n\nenter your choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
printf("\nenqueue operation\n");
if(r<0)
{
printf("\n\nThe queue is full\n");
}
else
{
printf("\nEnter the number: ");
scanf("%d",&n);
q[r--]=n;
}
break;
case 2:
printf("\ndequeue operation\n");
if(f==r)
{
printf("\nqueue is empty");
}
else
{
n=q[f--];
printf("\n%d is deleted from the queue\n",n);
}
break;
case 3:
printf("\n\ndisplay operation");
if(f==r)
{
printf("\n\nqueue is empty");
}
else
{
printf("\n\nThe elements in the queue are:");
for(i=f;i!=r;i--)
{
printf("%d\t",q[i]);
}
break;
case 4:
exit(0);
default:
printf("\nenter the correct option...");
break;
}
getch();
}
}
}
OUTPUT:

1.Enqueue
2.Dequeue
3.Display
4.Exit
Enter your choice:1
Enqueue Operation
Enter the number:52

Enter your choice:1


Enqueue Operation
Enter the number:23

Enter your choice: 1


Enqueue Operation
Enter the number:24

Enter your choice:3


52 23 24

Enter your choice:2


Dequeue Operation
52 is deleted from the list

Enter your choice:3


23 24

Enter your choice:4


/* REVERSE STRING USING STACK ADT */

#include<stdio.h>
#include<conio.h>
#define STACK_SIZE 80
int push(char value);
int pop(char *value);
void revstring(char *str);
int top=-1;
char stack[STACK_SIZE];
void main()
{
char str[STACK_SIZE];
clrscr();
printf("Enter the string\n");
gets(str);
reversestring(str);
printf("\n the reversed string is %s",str);
getch();
}
void reversestring(char *str)
{
int i;
push(NULL);
for(i=0;str[i]!=NULL;i++)
push(str[i]);
while(!=pop(str++));
}
int push(char value)
{
extern char stack[];
extern int top;
if(top==STACK_SIZE-1)
return -1;
stack[++top]=value;
return 0;
}
int pop(char *value)
{
extern char stack[];
extern int top;
if(top==-1)
return -1;
*value=stack[top--];
return 0;
}

OUTPUT:

Enter the string: REVERSE

The reversed string is: ESREVER


/* TOWERS OF HANOI */

#include<stdio.h>
#include<conio.h>
void towers_of_hanoi(int n,char *a,char *c,char *r);
void main()
{
int n;
clrscr();
printf("\n Enter the number of disc: ");
scanf("%d",&n);
towers_of_hanoi(n,"Tower1","Tower2","Tower3");
getch();
}
void towers_of_hanoi(int n,char *a,char *c,char *r)
{
if(n<=0)
return;
towers_of_hanoi(n-1,a,r,c);
printf("\n\t Move disc %d from %s to %s",n,a,r);
towers_of_hanoi(n-1,c,a,r);
}
OUTPUT:

Enter the number of discs: 3

Move disc 1 from tower 1 to tower 3


Move disc 2 from tower 1 to tower 2
Move disc 1 from tower 3 to tower 2
Move disc 3 from tower 1 to tower 3
Move disc 1 from tower 2 to tower 1
Move disc 2 from tower 2 to tower 3
Move disc 1 from tower 1 to tower 3
/* CHECKING BALANCED PARANTHESIS
USING ARRAY IMPLEMENTATION OF STACK
ADT */

#include<stdio.h>
#include<conio.h>
#define STACK_SIZE 80
int push(char value);
int pop(char*value);
void balpar(char*);
int top=-1;
char stack[STACK_SIZE];
int icount=0,rcount=0;
void main()
{
char str[STACK_SIZE];
clrscr();
printf("Enter the string: ");
gets(str);
balpar(str);
printf("%s",str);
}
void balpar(char*str)
{
int i;
push('/0');
for(i=0;str[i]!=NULL;i++)
push(str[i]);
while(!pop(str++));
if(icount==rcount)
printf("The expression is balanced");
else
printf("The expression is not balanced");
getch();
}
int push(char value)
{
int ch;
extern char stack[];
extern int top;
if(top==STACK_SIZE-1)
return-1;
stack[++top]=value;
return 0;
}
int pop(char*value)
{
extern char stack[];
extern int top;
if(top==-1)
return -1;
*value=stack[top--];
if(*value=='c')
{
icount++;
}
if(*value=='i')
{
rcount++;
}
return 0;
}

OUTPUT:

Enter the string: (A+B)*C)

The Expression is balanced.

Enter the string: ((A+B)*C

The Expression is not balanced.


/* INSERTION SORT */

#include<stdio.h>
#include<conio.h>
void insert(int a[],int n);
int i,j,n,temp,a[25];
void main()
{
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("\n Enter the elements: ");
for(i=0;i<n;i++)
{
scanf("%d",&a[i]);
}
insert(a,n);
printf("\n The sorted list:");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void insert(int a[],int n)
{
for(i=1;i<n;i++)
{
temp=a[i];
for(j=i;j>=1;j--)
{
if(temp<a[j-1])
a[j]=a[j-1];
else
break;
}
a[j]=temp;n
}
}

OUTPUT:

Enter the limit:5

Enter the elements : 25 30 18 47 10

The sorted list : 10 18 25 30 47


/* QUICK SORT */

#include<stdio.h>
#include<conio.h>
int i,j,n,pivot,a[20];
void quick(int a[],int left,int right);
void swap(int a[],int i,int j);
void main()
{
int i,n,a[20];
clrscr();
printf("Enter the limit");
scanf("%d",&n);
printf("Enter the element to sort");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
quick(a,0,n-1);
printf("The sorted list: ");
for(i=0;i<n;i++)
printf("\t %d",a[i]);
getch();
}
void quick(int a[],int first,int last)
{
if(first<last)
{
pivot=a[first];
i=first;
j=last;
while(i<j)
{
while(a[j]<=pivot&&&i<last)
i++;
while(a[j]>=pivot&&j>first)
j--;
if(i<j)
swap(a,first,j);
quick(a,first,j-1);
quick(a,j+1,last);
}
}
void swap(int a[],int i,int j)
{
int temp;
temp=a[i];
a[i]=a[j];
a[j]=temp;
}

OUTPUT:

Enter the limit:5


Enter the elements to sort: 26 33 18 45 11

The sorted list : 11 18 26 33 45


/* SHELL SORT */

#include<stdio.h>
#include<conio.h>
void shell(int a[],int n);
int i,j,n,k,temp,a[25];
void main()
{
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
printf("\nEnter the number: ");
for(i=0;i<n;i++)
scanf("%d",&a[i]);
printf("before sorting:\n");
for(j=0;j<n;j++)
printf("%d\n",a[j]);
shell(a,n);
printf("After Sorting:\n");
for(i=0;i<n;i++)
printf("%d\n",a[i]);
getch();
}
void shell(int a[],int n)
{
for(i=(n+1)/2;i>=1;i=i/2)
{
for(j=i;j<=n-1;j++)
{
temp=a[j];
k=j-i;
while(k>=0 && temp<=a[k])
{
a[k+i]=a[k];
k=k-i;
}
a[k+i]=temp;
}
}
}

OUTPUT:

Enter the limit: 5

Enter the number: 5 4 3 2 1

Before Sorting:
5
4
3
2
1
After Sorting:
1
2
3
4
5
* HEAP SORT */

#include<stdio.h>
#include<conio.h>
void main()
{
int r[50];
int n;
clrscr();
printf("Enter the limit: ");
scanf("%d",&n);
readelement(r,n);
getch();
}
readelement(int r[50],int n)
{
printf("Enter the element: \n");
for(i=1;i<=n;i++)
scanf("%d",&r[i]);
}
hsort(int r[10],int n)
{
int i,t;
for(i=n/2;i>0;i++)
adjust(r,i,n);
for(i=n-1;i>0;i++)
{
t=r[i+1];
r[i+1]=r[i];
r[i]=t;
adjust(r,j,i);
}
printf("The sorted list is:");
for(i=1;i<n;i++)
printf("%d",r[i]);
}
adjust(int r[50], int i,int n)
{
int j,t;
t=r[i];
j=2*i;
while(j<=n)
{
if(i<n && r[j]<r[j+1])
j=j+1;
for(i=1;i<n;i++)
printf("%d",r[i]);
}
}

OUTPUT:

Enter the limit: 7

Enter the numbers: 3 1 4 2 6 75

The sorted list is:


1
2
3
4
5
6
7
/* BINARY SEARCH TREE */

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
struct tree
{
int info;
struct tree *left,*right;
};
struct tree *root;
void insert(int item)
{
struct tree *temp;
temp=root;
if(root==NULL)
{
root=(struct tree*)malloc(sizeof(struct tree));
root->info=item;
root->left=NULL;
}
else
{
while((temp->info>item)&&(temp->left!=NULL))
temp=temp->left;
while((temp->info<item)&&(temp->right!
=NULL))
temp=temp->right;
if(temp->info>item)
{
temp->left=(struct tree*)malloc(sizeof(struct
tree));
temp->left->info=item;
temp->left->right=NULL;
temp->left->left=NULL;
}
else
{
temp->right=(struct tree*)malloc(sizeof(struct
tree));
temp->right->info=item;
temp->left->right=NULL;
temp->left->left=NULL;
}
}
}
void search(int item)
{
struct tree *temp;
temp=root;
while((temp->info>item)&&(temp->left!=NULL))
{
temp=temp->left;
if(temp->info==item)
break;
}
while((temp->info<item)&&(temp->right!=NULL))
temp=temp->right;
if(temp->info==item)
{
printf("\nsearch successfull");
}
else
{
printf("\nunsuccessful search");
}
}
void inorder(struct tree *t)
{
if(t!=NULL)
{
inorder(t->left);
printf("\n%d",t->info);
inorder(t->right);
}
}
void preorder(struct tree *t)
{
if(t!=NULL)
{
printf("\n%d",t->info);
preorder(t->left);
preorder(t->right);
}
}
void postorder(struct tree *t)
{
if(t!=NULL)
{
postorder(t->left);
postorder(t->right);
printf("\n%d",t->info);
}
}
void main()
{
int ch,item;
clrscr();
root=NULL;
do
{
printf("\n1.CREATION");
printf("\n2.INORDER");
printf("\n3.PREORDER");
printf("\n4.POSTORDER");
printf("\n5.SEARCH");
printf("\n6.EXIT");
printf("\n enter your choice:");
scanf("%d",&ch);
printf("%d",ch);
switch(ch)
{
case 1:
while(1)
{
printf("\nenter the limit(0-exit)");
scanf("%d",&item);
printf("%d",item);
if(item==0)
break;
insert(item);
}
break;
case 2:
inorder(root);
break;
case 3:
preorder(root);
break;
case 4:
postorder(root);
break;
case 5:
printf("enter the element to be searched");
scanf("%d",item);
search(item);
search(item);
break;
case 6:
printf("\nend of the operation");
break;
default:
printf("enter only 1 to 5");
}
}
while(ch<6);
getch();
}

Vous aimerez peut-être aussi