Vous êtes sur la page 1sur 35

/*Insertion ,Deletion and Traversal in Binary Search Tree*/

# include<stdio.h>
# include<conio.h>
# include<process.h>
# include<malloc.h>

struct node
{
int info;
struct node *lchild;
struct node *rchild;
}*root;

void display(struct node *ptr,int level);


void postorder(struct node *ptr);
void preorder(struct node *ptr);
void inorder(struct node *ptr);
void case_a(struct node *par,struct node *loc);
void case_b(struct node *par,struct node *loc);
void case_c(struct node *par,struct node *loc);
void del(int item);
void insert(int item);
void find(int item,struct node **par,struct node **loc);

void main()
{
clrscr();
int choice,num;
root=NULL;
while(1)
{
printf("\n");
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Inorder Traversal\n");
printf("4.Preorder Traversal\n");
printf("5.Postorder Traversal\n");
printf("6.Display\n");
printf("7.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1:
printf("Enter the number to be inserted : ");
scanf("%d",&num);
insert(num);
break;
case 2:
printf("Enter the number to be deleted : ");
scanf("%d",&num);
del(num);
break;
case 3:
inorder(root);
break;
case 4:
preorder(root);
break;
case 5:
postorder(root);
break;
case 6:
display(root,1);
break;
case 7:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch */
}/*End of while */
}/*End of main()*/

void find(int item,struct node **par,struct node **loc)


{
struct node *ptr,*ptrsave;

if(root==NULL) /*tree empty*/


{
*loc=NULL;
*par=NULL;
return;
}
if(item==root->info) /*item is at root*/
{
*loc=root;
*par=NULL;
return;
}
/*Initialize ptr and ptrsave*/
if(item)
ptr=root->lchild;
else
ptr=root->rchild;
ptrsave=root;

while(ptr!=NULL)
{
if(item==ptr->info)
{ *loc=ptr;
*par=ptrsave;
return;
}
ptrsave=ptr;
if(item)
ptr=ptr->lchild;
else
ptr=ptr->rchild;
}/*End of while */
*loc=NULL; /*item not found*/
*par=ptrsave;
}/*End of find()*/

void insert(int item)


{ struct node *tmp,*parent,*location;
find(item,&parent,&location);
if(location!=NULL)
{
printf("Item already present");
return;
}

tmp=(struct node *)malloc(sizeof(struct node));


tmp->info=item;
tmp->lchild=NULL;
tmp->rchild=NULL;

if(parent==NULL)
root=tmp;
else
if(item)
parent->lchild=tmp;
else
parent->rchild=tmp;
}/*End of insert()*/

void del(int item)


{
struct node *parent,*location;
if(root==NULL)
{
printf("Tree empty");
return;
}

find(item,&parent,&location);
if(location==NULL)
{
printf("Item not present in tree");
return;
}

if(location->lchild==NULL && location->rchild==NULL)


case_a(parent,location);
if(location->lchild!=NULL && location->rchild==NULL)
case_b(parent,location);
if(location->lchild==NULL && location->rchild!=NULL)
case_b(parent,location);
if(location->lchild!=NULL && location->rchild!=NULL)
case_c(parent,location);
free(location);
}/*End of del()*/

void case_a(struct node *par,struct node *loc )


{
if(par==NULL) /*item to be deleted is root node*/
root=NULL;
else
if(loc==par->lchild)
par->lchild=NULL;
else
par->rchild=NULL;
}/*End of case_a()*/

void case_b(struct node *par,struct node *loc)


{
struct node *child;

/*Initialize child*/
if(loc->lchild!=NULL) /*item to be deleted has lchild */
child=loc->lchild;
else /*item to be deleted has rchild */
child=loc->rchild;
if(par==NULL ) /*Item to be deleted is root node*/
root=child;
else
if( loc==par->lchild) /*item is lchild of its parent*/
par->lchild=child;
else /*item is rchild of its parent*/
par->rchild=child;
}/*End of case_b()*/

void case_c(struct node *par,struct node *loc)


{
struct node *ptr,*ptrsave,*suc,*parsuc;

/*Find inorder successor and its parent*/


ptrsave=loc;
ptr=loc->rchild;
while(ptr->lchild!=NULL)
{
ptrsave=ptr;
ptr=ptr->lchild;
}
suc=ptr;
parsuc=ptrsave;

if(suc->lchild==NULL && suc->rchild==NULL)


case_a(parsuc,suc);
else
case_b(parsuc,suc);

if(par==NULL) /*if item to be deleted is root node */


root=suc;
else
if(loc==par->lchild)
par->lchild=suc;
else
par->rchild=suc;

suc->lchild=loc->lchild;
suc->rchild=loc->rchild;
}/*End of case_c()*/

void preorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
printf("%d ",ptr->info);
preorder(ptr->lchild);
preorder(ptr->rchild);
}
}/*End of preorder()*/

void inorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
inorder(ptr->lchild);
printf("%d ",ptr->info);
inorder(ptr->rchild);
}
}/*End of inorder()*/

void postorder(struct node *ptr)


{
if(root==NULL)
{
printf("Tree is empty");
return;
}
if(ptr!=NULL)
{
postorder(ptr->lchild);
postorder(ptr->rchild);
printf("%d ",ptr->info);
}
}/*End of postorder()*/

void display(struct node *ptr,int level)


{
int i;
if ( ptr!=NULL )
{
display(ptr->rchild, level+1);
printf("\n");
for (i = 0; i < level; i++)
printf(" ");
printf("%d", ptr->info);
display(ptr->lchild, level+1);
}/*End of if*/
}/*End of display()*/

/
************************************OUTPUT****************************
*******

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 23

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 234

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 345

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 2

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 43

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 1
Enter the number to be inserted : 467

1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 6

23
234
345
2
43
467
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 3
467 43 2 345 234 23
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 4
23 234 345 2 43 467
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 5
467 43 2 345 234 23
1.Insert
2.Delete
3.Inorder Traversal
4.Preorder Traversal
5.Postorder Traversal
6.Display
7.Quit
Enter your choice : 7
*//* Program of circular queue using array*/
# include<stdio.h>
#include<conio.h>
#include<process.h>
# define MAX 5

int cqueue_arr[MAX];
int front = -1;
int rear = -1;

void insert();
void del();
void display();

main()
{
clrscr();
int choice;
while(1)
{
printf("1.Insert\n");
printf("2.Delete\n");
printf("3.Display\n");
printf("4.Quit\n");
printf("Enter your choice : ");
scanf("%d",&choice);

switch(choice)
{
case 1 :
insert();
break;
case 2 :
del();
break;
case 3:
display();
break;
case 4:
exit(1);
default:
printf("Wrong choice\n");
}/*End of switch*/
}/*End of while */
}/*End of main()*/
void insert()
{
int added_item;
if((front == 0 && rear == MAX-1) || (front == rear+1))
{
printf("Queue Overflow \n");
return;
}
if (front == -1) /*If queue is empty */
{
front = 0;
rear = 0;
}
else
if(rear == MAX-1)/*rear is at last position of queue */
rear = 0;
else
rear = rear+1;
printf("Input the element for insertion in queue : ");
scanf("%d", &added_item);
cqueue_arr[rear] = added_item ;
}/*End of insert()*/

void del()
{
if (front == -1)
{
printf("Queue Underflow\n");
return ;
}
printf("Element deleted from queue is : %d\n",cqueue_arr[front]);
if(front == rear) /* queue has only one element */
{
front = -1;
rear=-1;
}
else
if(front == MAX-1)
front = 0;
else
front = front+1;
}/*End of del() */

void display()
{
int front_pos = front,rear_pos = rear;
if(front == -1)
{
printf("Queue is empty\n");
return;
}
printf("Queue elements :\n");
if( front_pos <= rear_pos )
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
else
{
while(front_pos <= MAX-1)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
front_pos = 0;
while(front_pos <= rear_pos)
{
printf("%d ",cqueue_arr[front_pos]);
front_pos++;
}
}/*End of else */
printf("\n");
}/*End of display() */

/
*********************************OUTPUT*******************************
******

1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 345
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 365
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 234
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 1
Input the element for insertion in queue : 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
34 345 365 234 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 2
Element deleted from queue is : 34
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 3
Queue elements :
345 365 234 2
1.Insert
2.Delete
3.Display
4.Quit
Enter your choice : 4
*/program of double linked list/*
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>

struct dllist {
int number;
struct dllist *next;
struct dllist *prev;
};

struct dllist *head, *tail;

void append_node(struct dllist *lnode);


void insert_node(struct dllist *lnode, struct dllist *after);
void remove_node(struct dllist *lnode);

void main(void) {
clrscr();

struct dllist *lnode;


int i = 0;

/* add some numbers to the double linked list */


for(i = 0; i <= 5; i++) {
lnode = (struct dllist *)malloc(sizeof(struct dllist));
lnode->number = i;
append_node(lnode);
}

/* print the dll list */

printf("\n->>");
for(lnode = head; lnode != NULL; lnode = lnode->next) {
printf("%d->", lnode->number);
}

/* destroy the dll list */


while(head != NULL)
remove_node(head);

getch();
}

void append_node(struct dllist *lnode) {


if(head == NULL) {
head = lnode;
lnode->prev = NULL;
} else {
tail->next = lnode;
lnode->prev = tail;
}

tail = lnode;
lnode->next = NULL;
}

void insert_node(struct dllist *lnode, struct dllist *after) {


lnode->next = after->next;
lnode->prev = after;

if(after->next != NULL)
after->next->prev = lnode;
else
tail = lnode;

after->next = lnode;
}

void remove_node(struct dllist *lnode) {


if(lnode->prev == NULL)
head = lnode->next;
else
lnode->prev->next = lnode->next;

if(lnode->next == NULL)
tail = lnode->prev;
else
lnode->next->prev = lnode->prev;
}

/
*********************************OUTPUT*******************************
******

->>0->1->2->3->4->5->
*//*program for heap sort*/

#include<stdio.h>
#include<conio.h>

void restoreHup(int*,int);
void restoreHdown(int*,int,int);

void main()
{
clrscr();
int a[20],n,i,j,k;
printf("Enter the number of elements to sort : ");
scanf("%d",&n);

printf("Enter the elements :");


for(i=1;i<=n;i++){
scanf("%d",&a[i]);
restoreHup(a,i);
}

j=n;
for(i=1;i<=j;i++)
{
int temp;
temp=a[1];
a[1]=a[n];
a[n]=temp;
n--;
restoreHdown(a,1,n);
}

n=j;

printf("\Here is it...");
for(i=1;i<=n;i++)
printf("%4d",a[i]);
getch();
}
void restoreHup(int *a,int i)
{
int v=a[i];

while((i>1)&&(a[i/2]<v))
{
a[i]=a[i/2];
i=i/2;
}
a[i]=v;
}

void restoreHdown(int *a,int i,int n)


{
int v=a[i];
int j=i*2;
while(j<=n)
{
if((j<n)&&(a[j]<a[j+1]))
j++;
if(a[j]<a[j/2]) break;

a[j/2]=a[j];
j=j*2;
}
a[j/2]=v;
}

/
**********************************OUTPUT******************************
******

Enter the number of elements to sort : 6


Enter the elements :45
65
23
467
2
456
Here is it... 2 23 45 65 456 467
*/ insertion sort/*
#include<conio.h>
#include<stdio.h>
void insertsort(int *,int);
void main()
{
int *a,i,n;
clrscr();
printf("\nEnter the size of the array..:");
scanf("%d",&n);

for(i=0;i<n;i++){
printf("\nEnter a[%d]..:",i);
scanf("%d",&a[i]);
}

insertsort(a,n);
printf("\nThe sorted order is..:");

for(i=0;i<n;i++) printf("%d ",a[i]);

getch();
}
void insertsort(int *a,int n)
{
int i,temp,j;
for(i=1;i<n;i++) {
temp=a[i];
for(j=i-1;j>=0;j--)
{
if(a[j]>temp)
a[j+1]=a[j];
else break;
}
a[j+1]=temp;
}
}
/
************************************OUTPUT****************************
******

Enter the size of the array..:6

Enter a[0]..:34

Enter a[1]..:234

Enter a[2]..:2

Enter a[3]..:455

Enter a[4]..:4

Enter a[5]..:56

The sorted order is..:2 4 34 56 234 455


*//*Shell Sort */
#include<stdio.h>
#include<conio.h>

void shellsort(int a[],int n)


{
int j,i,k,m,mid;
for(m = n/2;m>0;m/=2)
{
for(j = m;j< n;j++)
{
for(i=j-m;i>=0;i-=m)
{
if(a[i+m]>=a[i])
break;
else
{
mid = a[i];
a[i] = a[i+m];
a[i+m] = mid;
}
}
}
}
}

main()
{
int a[10],i,n;
clrscr();

printf("Enter The number Of Elements\t: ");


scanf("%d",&n);
for(i=0;i< n;i++)
{
printf("\nElement %d\t: ",i+1);
scanf("%d",&a[i]);
}

printf("\nArray Befor Sorting : ");


for(i=0;i< n;i++)
printf("%5d",a[i]);
shellsort(a,n);

printf("\nArray After Sorting : ");


for(i=0;i< n;i++)
printf("%5d",a[i]);
getch();
return 0;
}

/*************************************
OUTPUT*******************************************

Enter The number Of Elements : 5

Element 1 : 21

Element 2 : 36

Element 3 : 54

Element 4 : 2

Element 5 : 0

Array Befor Sorting : 21 36 54 2 0


Array After Sorting : 0 2 21 36 54
*//* Singly Link list */
# include<stdio.h>
# include<conio.h>
# include<alloc.h>
# include<stdlib.h>
struct node
{ int data;
struct node *link;
};
void append(struct node **,int);
void in_begin(struct node **,int);
void del(struct node **,int);
void in_middle(struct node **,int,int);
int count(struct node *);
void display(struct node *);
void main()
{clrscr();
struct node *p; /* p can be said as the head or a start ptr */
p=NULL;
/* Printing the menu */
int num,loc;
char choice;
do
{ clrscr();;
printf("\n\n1.Create \\ Appending The List");
printf("\n2.Insert Node At Begining");
printf("\n3.Insert Node In Middle");
printf("\n4.Deleting a Node");
printf("\n5.Counting The No Of Nodes");
printf("\n6.Displaying the list");
printf("\n7.Exit");
oper:
gotoxy(1,15);printf(" ");
gotoxy(1,11);printf("\n\nEnter ur Choice : ");
choice=getch();
switch(choice)
{ case '1':
char ans;
do
{ printf("Enter any number : ");
scanf("%d",&num);
append(&p,num);
printf("Enter more (y/n) :");
fflush(stdin);
ans=getchar();
}while(ans !='n');
break;
case '2':
printf("Enter The Data : ");
scanf("%d",&num);
in_begin(&p,num);
break;

case '3':
printf("\nEnter The Position :");
scanf("%d",&loc);
printf("\nEnter The Data : ");
scanf("%d",&num);
in_middle(&p,loc,num);
break;

case '4':
printf("\nEnter The Data u Want To Delete : ");
scanf("%d",&num);
del(&p,num);
break;

case '5':
printf("\nThe No Of Nodes Are %d",count(p));
getch();
break;

case '6':
display(p);
getch();
break;

case '7':
printf("\n\nQuiting.......");
getch();
exit(0);
break;
default:
gotoxy(1,15);printf("Invalid choice.Please Enter Correct Choice");
getch();
goto oper;

}
}while(choice !=7);
}

void append(struct node **q,int num)


{ struct node *temp,*r;
temp = *q;
if(*q==NULL)
{ temp = (struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=NULL;
*q=temp;
}
else
{ temp = *q;
while(temp->link !=NULL)
{ temp=temp->link;
}
r = (struct node *)malloc(sizeof(struct node));
r->data=num;
r->link=NULL;
temp->link=r;
}
}

void display(struct node *q)


{ if(q==NULL)
{ printf("\n\nEmpty Link List.Can't Display The Data");
getch();
goto last;
}
while(q!=NULL)
{ printf("\n%d",q->data);
q=q->link;
}
last:
}

int count(struct node *q)


{ int c=0;
if(q==NULL)
{ printf("Empty Link List.\n");
getch();
goto last;
}
while(q!=NULL)
{ c++;
q=q->link;
}
last:
return c;

void in_begin(struct node **q,int num)


{ struct node *temp;
if(*q==NULL)
{ printf("Link List Is Empty.Can't Insert.");
getch();
goto last;
}
else
{ temp=(struct node *)malloc(sizeof(struct node));
temp->data=num;
temp->link=*q;
*q=temp; /* pointing to the first node */
}
last:
getch();
}

void in_middle(struct node **q,int loc,int num)


{ struct node *temp,*n;
int c=1,flag=0;
temp=*q;
if(*q==NULL)
{ printf("\n\nLink List Is Empty.Can't Insert.");
getch();
goto last;
}
else
while(temp!=NULL)
{ if(c==loc)
{ n = (struct node *)malloc(sizeof(struct node));
n->data=num;
n->link=temp->link;
temp->link=n;
flag=1;
}
c++;
temp=temp->link;
}
if(flag==0)
{ printf("\n\nNode Specified Doesn't Exist.Cant Enter The Data");
getch();
}
else
{ printf("Data Inserted");
getch();
}
last:
getch();
}

void del(struct node**q,int num)


{ if(*q==NULL)
{ printf("\n\nEmpty Linked List.Cant Delete The Data.");
getch();
goto last;
}
else
{
struct node *old,*temp;
int flag=0;
temp=*q;
while(temp!=NULL)
{ if(temp->data==num)
{ if(temp==*q) /* First Node case */
*q=temp->link; /* shifted the header node */
else
old->link=temp->link;

free(temp);
flag=1;
}
else
{ old=temp;
temp=temp->link;
}
}
if(flag==0)
printf("\nData Not Found...");
else
printf("\nData Deleted...Tap a key to continue");
getch();
}
last:
getch();
}

/
*********************************OUTPUT*******************************
******

1.Create \ Appending The List


2.Insert Node At Begining
3.Insert Node In Middle
4.Deleting a Node
5.Counting The No Of Nodes
6.Displaying the list
7.Exit

Enter ur Choice : 1 Enter any number : 45


Enter more (y/n) :y
Enter any number : 67
Enter more (y/n) :y
Enter any number : 70
Enter more (y/n) :y
Enter any number : 78
Enter more (y/n) :y
Enter any number : 89
Enter more (y/n) :y
Enter any number : 90
Enter more (y/n) :n

1.Create \ Appending The List


2.Insert Node At Begining
3.Insert Node In Middle
4.Deleting a Node
5.Counting The No Of Nodes
6.Displaying the list
7.Exit

Enter ur Choice : 4
Enter The Data u Want To Delete : 67
Data Deleted...Tap a key to continue

1.Create \ Appending The List


2.Insert Node At Begining
3.Insert Node In Middle
4.Deleting a Node
5.Counting The No Of Nodes
6.Displaying the list
7.Exit

Enter ur Choice : 5

The No Of Nodes Are 5

1.Create \ Appending The List


2.Insert Node At Begining
3.Insert Node In Middle
4.Deleting a Node
5.Counting The No Of Nodes
6.Displaying the list
7.Exit

Enter ur Choice : 6
45
70
78
89
90

*/
*/ transpose of sparse matrices/*
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
int a[10][10],b[10][10];
int m,n,p,q,t,col;
int i,j;

printf("enter the no of row and columns :\n");


scanf("%d %d",&m,&n);

// assigning the value of matrix

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("a[%d][%d]= ",i,j);
scanf("%d",&a[i][j]);
}
}
printf("\n\n");

//displaying the matrix

printf("\n\nThe matrix is :\n\n");

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{
printf("%d",a[i][j]);
}
printf("\n");
}

t=0;

printf("\n\nthe non zero value matrix are :\n\n");

for(i=1;i<=m;i++)
{
for(j=1;j<=n;j++)
{

// accepting only non zero value


if(a[i][j]!=0)
{
t=t+1;
b[t][1]=i;
b[t][2]=j;
b[t][3]=a[i][j];
} }
}

// displaying the matrix of non-zero value

printf("\n");
printf("a[0 %d %d %d\n",m,n,t);

for(i=1;i<=t;i++)
{
printf("a[%d %d %d %d \n",i,b[i][1],b[i][2],b[i][3]);
}

b[0][1]=n; b[0][2]=m; b[0][3]=t;


q=1;

// transpose of the matrix

printf("\n\nthe transpose of the matrix :\n ");

if(t>0)
{
for(i=1;i<=n;i++)
{
for(j=1;j<=t;j++)
{
if(b[j][2]==i)
{
a[q][1]=b[j][2]; a[q][2]=b[j][1];
a[q][3]=b[j][3]; q=q+1;
} }
} }

printf("\n\n");
printf("a[0 %d %d %d\n",b[0][1],b[0][2],b[0][3]);

for(i=1;i<=t;i++)
{
printf("a[%d %d %d %d\n",i,a[i][1],a[i][2],a[i][3]);
}
getch();
}

/
*********************************OUTPUT*******************************
******

enter the no of row and columns :


3
3
a[1][1]= 12
a[1][2]= 23
a[1][3]= 1
a[2][1]= 2
a[2][2]= 34
a[2][3]= 22
a[3][1]= 36
a[3][2]= 67
a[3][3]= 15

The matrix is :

12231
23422
366715

the non zero value matrix are :

a[0 3 3 9
a[1 1 1 12
a[2 1 2 23
a[3 1 3 1
a[4 2 1 2
a[5 2 2 34
a[6 2 3 22
a[7 3 1 36
a[8 3 2 67
a[9 3 3 15

the transpose of the matrix :

a[0 3 3 9
a[1 1 1 12
a[2 1 2 2
a[3 1 3 36
a[4 2 1 23
a[5 2 2 34
a[6 2 3 67
a[7 3 1 1
a[8 3 2 22
a[9 3 3 15

*/
*/ stack using array/*
# include<stdio.h>
# include<conio.h>
# include<process.h>
void display();
struct stacks
{
int a[100],f;
}d;

void display()
{
printf("the stack is\n");
for(int i=d.f-1;i>=0;i--)
printf("%d\n",d.a[i]);
}

void main()
{
clrscr();
int choice,number;
d.f=0;
do
{
printf("1-push\n");
printf("2-pop\n");
printf("3-exit\n");
printf("enter the choice\n");
scanf("%d",&choice);
switch(choice)
{
case 1:printf("enter the number\n");
scanf("%d",&number);
{
if(d.f==100)
printf("overflow\n");
else
d.a[d.f++]=number;
}
display();
break;
case 2:int number=-50;
if(d.f==0)
printf("underflow\n");
else
number=d.a[--d.f];
if(number!=-50)
printf("the number is deleted\n");
display();
break;
case 3:exit(0);
default:printf("wrong choice\n");
}
}while(choice!=3);
getch();
}

/
**********************************OUTPUT******************************
*******

1-push
2-pop
3-exit
enter the choice
1
enter the number
65
the stack is
65
54
23
1-push
2-pop
3-exit
enter the choice
1
enter the number
89
the stack is
89
65
54
23
1-push
2-pop
3-exit
enter the choice
1
enter the number
90
the stack is
90
89
65
54
23
1-push
2-pop
3-exit
enter the choice
2
the number is deleted
the stack is
89
65
54
23
1-push
2-pop
3-exit
enter the choice
3
*/

Vous aimerez peut-être aussi