Archive for the ‘Lab 9’ Category

/* C program to implement 2 stacks using 1 array
written by- Shivam Rana
Date- 14/4/12 */
#include<stdio.h>
#include<stdlib.h>
void main()
{
int n,top1,top2,ch=1,a,i,arr[100];
printf("Enter size of array you want to use\n");
scanf("%d",&n);
top1=-1;
top2=n;
while(ch!=0)
{
printf("What do u want to do?\n");
printf("1.Push element in stack 1\n");
printf("2.Push element in stack 2\n");
printf("3.Pop element from stack 1\n");
printf("4.Pop element from stack 2\n");
printf("5.Display stack 1\n");
printf("6.Display stack 2\n");
printf("0.EXIT\n");
scanf("%d",&ch);
switch(ch)
{
case 1:
{
printf("Enter the element\n");
scanf("%d",&a);
if(top1!=(top2-1))
arr[++top1]=a;
else
printf("Overflow\n");
break;
}
case 2:
{
printf("Enter the element\n");
scanf("%d",&a);
if(top2!=(top1+1))
arr[--top2]=a;
else
printf("Overflow\n");
break;
}
case 3:
{
if(top1==-1)
printf("Stack1 is empty\n");
else
{
a=arr[top1--];
printf("%d\n",a);
}
break;
}
case 4:
{
if(top2==n)
printf("Stack2 is empty\n");
else
{
a=arr[top2++];
printf("%d\n",a);
}
break;
}
case 5:
{
if(top1==-1)
printf("Stack1 is empty\n");
else
{
printf("Stack1 is-->>\n");
for(i=0;i<=top1;i++)
printf("%d ",arr[i]);
printf("\n");
}
break;
}
case 6:
{
if(top2==n)
printf("Stack2 is empty\n");
else
{
printf("Stack2 is-->>\n");
for(i=(n-1);i>=top2;i--)
printf("%d ",arr[i]);
printf("\n");
}
break;
}
case 0:
break;
}
}
}
Advertisements
/*C code to implement stack using 2-d array
written by- Shivam Rana*/
#include<stdio.h>
#include<stdlib.h>
int stack[5][5];
int x=0,y=-1;
void push(int);
int pop(void);
void display(void);
int main()
{
	int ch=-1,n,i;
	while(ch!=0)
	{
		printf("Enter your choice\n");
		printf("1.Push\n");
		printf("2.Pop\n");
		printf("3.Display\n");
		printf("0.EXIT\n");
		scanf("%d",&ch);
		switch(ch){
			case 1: 
			{
				printf("Enter number\n");
				scanf("%d",&n);
				push(n);
				break;
			}
			case 2:
			{
				i=pop();
				if(i==-1)
					printf("Empty\n");
				else
				printf("Popped element is->>>> %d",i);
				break;
			}
			case 3: 
			{
				display();
				break;
			}
			case 4:
				break;
		}
	}
	return 0;
}
void push(int n)
{
	if(y==4)
	{
		x++;
		y=-1;
	}
	if(x==5)
		printf("Overflow\n");
	else
	{
	stack[x][++y]=n;
	
	}
}
int pop(void)
{
	if(x==0&&y==-1)
		return -1;
	else
	{
		int i=stack[x][y--];
		if(y==-1)
		{
			x--;
			y=4;
		}
		return i;
	}
}
void display(void)
{
	printf("x=%d y=%d\n",x,y);
	int i,j;
	if(x==0&&y==-1)
		printf("Empty\n");
	else
	for(i=0;i<=x;i++)
		{
			if(i==x)
		for(j=0;j<=y;j++)
			printf("%d ",stack[i][j]);
			else
				for(j=0;j<=4;j++)
						printf("%d ",stack[i][j]);
		}

}

C code to implement binary tree

Posted: April 28, 2012 in Lab 9
/*C code to implement binary tree
Copied from College server 😀 */
#include<stdio.h>
#include<stdlib.h>
struct rec
{
	long num;
	struct rec *left;
	struct rec *right;
};
struct rec *tree=NULL;
struct rec *insert(struct rec *tree,long num);
void preorder(struct rec *tree);
void inorder(struct rec *tree);
void postorder(struct rec *tree);
int select(void);
int count=1;
int main()
{
	int choice;
	long digit;
	do
	 {
	   choice=select();
	   switch(choice)
	      {
		case 1: puts("Enter integer: To quit enter 0");
			scanf("%ld",&digit);
			while(digit!=0)
			    {
				tree=insert(tree,digit);
				scanf("%ld",&digit);
			    }continue;
		case 2: puts("\npreorder traversing TREE");
			preorder(tree);continue;
		case 3: puts("\ninorder traversing TREEE");
			inorder(tree);continue;
		case 4: puts("\npostorder traversing TREE");
			postorder(tree);continue;
		case 5: puts("END");exit(0);
		}
	}while(choice!=5);
	return 0;
}
int select(void)
{
int selection;
	do
	  {
		puts("Enter 1: Insert a node in the BT");
		puts("Enter 2: Display(preorder)the BT");
		puts("Enter 3: Display(inorder)the BT");
		puts("Enter 4: Display(postorder)the BT");
		puts("Enter 5: END");
		puts("Enter your choice");
		scanf("%d",&selection);
		     if((selection<1)||(selection>5))
			{
			  puts("wrong choice:Try again");
			  }
	   }while((selection<1)||(selection>5));
	   return (selection);
}
struct rec *insert(struct rec *tree,long digit)
{
	if(tree==NULL)
	  {
		tree=(struct rec *)malloc(sizeof(struct rec));
		tree->left=tree->right=NULL;
		tree->num=digit;count++;
	  }
else
	if(count%2==0)
	tree->left=insert(tree->left,digit);
else
	tree->right=insert(tree->right,digit);
	return(tree);
}
void preorder(struct rec *tree)
{
	if(tree!=NULL)
	  {
		printf("%12ld\n",tree->num);
		preorder(tree->left);
		preorder(tree->right);
	  }
}
void inorder(struct rec *tree)
{
	if(tree!=NULL)
	    {
		inorder(tree->left);
		printf("%12ld\n",tree->num);
		inorder(tree->right);
	    }
}
void postorder(struct rec *tree)
{
	if(tree!=NULL)
	  {
		postorder(tree->left);
		postorder(tree->right);
		printf("%12ld\n",tree->num);
	  }

}
/*C code to solve tower of hanoi recursively
written by- Shivam Rana*/
#include < stdio.h >
int N=3,N1=1,N2=2,N3=3;
void  hanoi (int n, int current, int destination, int medium)
{
	if(n!=0)
	{
		hanoi(n-1, current, medium, destination);
			printf( "move disk %c from %i to %i\n", (char)(n+'A'-1), current, destination);

		hanoi(n-1, medium, destination, current);
	}	
}

int main (void)
{ 	
		hanoi(N, N1, N3, N2);
	return 0;
}


/*C code to solve tpwer of hanoi iteratively
Written by- Shivam Rana*/
#include<stdio.h>
#include<stdlib.h>
#include<math.h>
#define pf printf
#define sf scanf
int p1[100],p2[100],p3[100],top1=0,top2=0,top3=0;
 void pushp1(int a)
{
	if(a!=0)
	p1[top1++]=a;
}
int popp1(void)
{
	if(top1==0)
		return 0;
	else
		return p1[--top1];
}
void displayp1(void)
{
	int i;
	pf("Tower 1 ->> ");
	if(top1==0)
		pf("Empty");
	else
	for(i=0;i<top1;i++)
	{
		pf("%d ",p1[i]);
	}
	pf("\n");
}
 void pushp2(int a)
{
	if(a!=0)
	p2[top2++]=a;
}
int popp2(void)
{
	if(top2==0)
		return 0;
	else
		return p2[--top2];
}
void displayp2(void)
{
	int i;
	pf("Tower 2->> ");
	if(top2==0)
		pf("Empty");
	else
	for(i=0;i<top2;i++)
	{
		pf("%d ",p2[i]);
	}
	pf("\n");
}
 void pushp3(int a)
{
	if(a!=0)
	p3[top3++]=a;
}
int popp3(void)
{
	if(top3==0)
		return 0;
	else
		return p3[--top3];
}
void displayp3(void)
{
	int i;
	pf("Tower 3 ->> ");
	if(top3==0)
		pf("Empty");
	else
	for(i=0;i<top3;i++)
	{
		pf("%d ",p3[i]);
	}
	pf("\n");
}
int main()
{
	int n,i,x,a,b;
	pf("Enter no. of discs\n");
	sf("%d",&n);
	for(i=0;i<n;i++)
		pushp1(n-i);
	x=pow(2,n)-1;
	displayp1();
	displayp2();
	displayp3();
	for(i=1;i<=x;i++)
	{
		if(i%3==1)
		{
		a=popp1();
		b=popp3();
		if(a==0)
			{
				pushp1(b);
			}
			else
				if(b==0)
				pushp3(a);
			else
				if(a>b)
			{
				pushp1(a);
				pushp1(b);
			}
			else
			{
				pushp3(b);
				pushp3(a);
			}
			displayp1();
			displayp2();
			displayp3();
			continue;
		}
else
if(i%3==2)
		{
		a=popp1();
		b=popp2();
		if(a==0)
			{
				pushp1(b);
			}
			else
				if(b==0)
				pushp2(a);
			else
				if(a>b)
			{
				pushp1(a);
				pushp1(b);
			}
			else
			{
				pushp2(b);
				pushp2(a);
			}
			displayp1();
			displayp2();
			displayp3();
			continue;
		}
else
if(i%3==0)
		{
		a=popp2();
		b=popp3();
		if(a==0)
			{
				pushp2(b);
			}
			else
				if(b==0)
				pushp3(a);
			else
				if(a>b)
			{
				pushp2(a);
				pushp2(b);
			}
			else
			{
				pushp3(b);
				pushp3(a);
			}
			displayp1();
			displayp2();
			displayp3();
			continue;
		}	
	}
	pf("Complete\n");
displayp1();
	displayp2();
	displayp3();
	return 0;
}