Professional Documents
Culture Documents
#include "stdio.h"
#include "conio.h"
#include "stdlib.h"
int queue[5],front=-1,rear=-1;
void insert()
{
int n;
printf("enter number to be entered");
scanf("%d",&n);
if(rear==-1)
{
rear=front=0;
queue[rear]=n;
}
else
if(rear==4)
{
printf("queue is full");
}
else
queue[++rear]=n;
}
void del()
{
if(front==-1)
printf("queue is empty");
else
if(front==rear)
{
printf("last element entered is %d",queue[front]);
front=rear=-1;
}
else
printf("%d",queue[front++]);
}
void main()
{
int k=1,num;
while(k==1)
{
printf("\n1 insert\n");
printf("2 delete\n");
printf("3 exit");
scanf("%d",&num);
if(num==1)
{
insert();
}
if(num==2)
{
del();
}
if(num==3)
{
k=0;
exit(0);
}
}
}
- 2. Infix to postfix
#include "stdio.h"
#include "conio.h"
#include "ctype.h"
char stack[100];
int tos=-1;
void push(char n)
{
if(tos==99)
printf("stack is overflow");
else
stack[++tos]=n;
}
char pop()
{
char ch;
if(tos==-1)
printf("stack is empty");
else
{
ch=stack[tos];
tos--;
}
return ch;
}
token=infix[i];
while(token!='\0')
{
if(isalnum(token))
postfix[p++]=token;
else
if(token=='(')
push('(');
else
if(token==')')
{
while(stack[tos]!='(')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}
else
if(token=='{')
push('{');
else
if(token=='}')
{
while(stack[tos]!='{')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}
else
if(token=='[')
push('[');
else
if(token==']')
{
while(stack[tos]!='[')
{
ch=pop();
postfix[p++]=ch;
}
ch=pop();
}
else
{
if(isp(stack[tos])<icp(token))
push(token);
else
{
while(isp(stack[tos])>=icp(token))
{
ch=pop();
postfix[p++]=ch;
}
push(token);
}
}
i++;
token=infix[i];
}
while(tos!=-1)
{
ch=pop();
postfix[p++]=ch;
}
printf("%s",postfix);
}
void main()
{
char infix[100];
printf("\n enter infix string");
gets(infix);
in_postfix(infix);
}
- 3. Add in Array
#include "stdio.h"
#include "conio.h"
void main()
{
int i,arr[10],num,n;
printf("enter 9 numbers in the array of 10\n");
for(i=0;i<9;i++)
{
printf("\n%d -----",i+1);
scanf ("%d",&arr[i]);
}
printf("\nEnter position where you want to add a number");
scanf("%d",&num);
for(i=9;i>num-1;i--)
{
arr[i]=arr[i-1];
}
printf("enter a number to be enter at %d postion",num);
scanf("%d",&n);
arr[num-1]=n;
void main()
{
int i,arr[10],num;
printf("enter 10 numbers in the array\n");
for(i=0;i<10;i++)
{
printf("\n%d -----",i+1);
scanf ("%d",&arr[i]);
}
printf("\nEnter position where you want to delete");
scanf ("%d",&num);
for(i=num-1;i<10;i++)
{
arr[i]=arr[i+1];
}
#include "stdio.h"
#include "conio.h"
void main()
{
int a[10],i,j,t;
printf("enter 10 numbers\n");
for(i=0;i<10;i++)
{
printf("%d element = ",i+1);
scanf ("%d",&a[i]);
}
for(j=0;j<9;j++)
{
for(i=0;i<9;i++)
{
if(a[i]>a[i+1])
{
t=a[i+1];
a[i+1]=a[i];
a[i]=t;
}
}
}
printf("The array after Bubble Sort\n");
for(i=0;i<10;i++)
{
printf("%d\n",a[i]);
}
}
t[j++]=0;
printf("The reversed string is %s ",t);
}
void strcon(char s[], char s1[])
{
int k=0,i;
char s2[MAX];
for(i=0;s[i]!=0;i++)
{
s2[k++]=s[i];
}
s2[k++]=' ';
for(i=0;s1[i]!=0;i++)
{
s2[k++]=s1[i];
}
s2[k++]=0;
printf("concatted string is %s",s2);
}
void main()
{
int num,ch=1;
char s[MAX],s1[MAX];
while(ch==1)
{
if(num==1)
{
printf("enter a string");
_flushall();
gets(s);
strln(s);
}
else if(num==2)
{
printf("enter a string");
_flushall();
gets(s);
strrev(s);
}
else if(num==3)
{
_flushall();
printf("\n\n\nenter String one");
gets(s);
printf("\n enter String two");
gets(s1);
strcon(s,s1);
}
else if(num==4)
{
exit(0);
}
printf("\n\n\ndo you want to continue\n");
printf("1 to continue 2 to exit");
scanf("%d",&ch);
}
}
- 8. Stack Pop and Push
#include"stdio.h"
#include"conio.h"
#include"stdlib.h"
int stack[10],tos=-1;
void push()
{
int num;
if(tos==9)
printf("stack is overflow");
else
printf("enter number to be entered : ");
scanf("%d",&num);
stack[++tos]=num;
}
void pop()
{
if(tos==-1)
printf(" stack is empty ");
else
printf(" num poped is %d\n ",stack[tos--]);
}
void traverse()
{
int i;
if(tos==-1)
printf(" stack is empty ");
else
for(i=0;i<=tos;i++)
printf(" %d\n",stack[i]);
}
void main()
{
int ch=1,num;
while(ch)
{
printf("press 1 to push element\n");
printf("press 2 to pop element\n");
printf("press 3 to traverse elements\n");
printf("press 4 to exit\n");
printf("enter your choice ");
scanf("%d",&num);
if(num==1)
push();
else if(num==2)
pop();
else if(num==3)
traverse();
else
exit(1);
flag=0;
for(i=0;i<10;i++)
{
if(a[i]==num)
{
flag=1;
break;
}
}
if(flag==1)
printf("\n%d found in array\n",num);
if(flag==0)
printf("\n%d does not exit\n",num);
printf("\n\ndo you want to continue searching\n");
printf("\n\nenter 1 to continue 0 to exit");
scanf("%d",&ch);
}
}
- 11. Link List
#include "stdio.h"
#include "conio.h"
#include "malloc.h"
#include "stdlib.h"
struct node
{
int data;
struct node *next;
};
node *head=NULL;
node *createnode()
{
node *temp;
temp=(node *)malloc(sizeof(node));
temp->next=NULL;
prev->next=temp;
prev=temp;
void insertbef()
{
node *temp,*prev;
int n;
printf("\n enter the data before which you want to insert a node ");
scanf("%d",&n);
if(head->data==n)
{
temp=createnode();
temp->next=head;
head=temp;
}
else
{
for(prev=head;prev->next=NULL;prev=prev->next)
if(prev->next->data==n)
{
temp=createnode();
temp->next=prev->next;
prev->next=temp;
return;
}
}
printf("\n data not found");
}
void main()
{
int ch;
while(1)
{
printf(" enter 1 to create list\n");
printf("enter 2 to display list\n");
printf("enter 3 to delete list node\n");
printf("enter 4 to insert before\n");
printf("enter 5 to insert after\n");
printf("enter 0 to exit ");
scanf("%d",&ch);
switch(ch)
{
case 1:
createlist();
break;
case 2:
display();
break;
case 3:
del();
break;
case 4:
insertbef();
break;
case 5:
insertaft();
break;
case 0:
exit(0);
break;
default:
printf(" \nInvalid choice ");
}
}
}
#include <stdio.h>
#include <ctype.h>
#include <conio.h>
int stack[100],tos=-1;
void push(char i)
{
if(tos==99)
printf("Stack is full");
else
stack[++tos]=i;
}
int pop()
{
int ch;
if(tos==-1)
printf("stack is empty");
else
ch=stack[tos--];
return ch;
}
void eval(char post[])
{
int i,t1,t2,t;
for(i=0;post[i]!=0;i++)
{
if(isalnum(post[i]))
{
push(post[i]-48);
}
else
{
t1=pop();
t2=pop();
if(post[i]=='+')
{
t=t1+t2;
}
else if(post[i]=='-')
{
t=t2-t1;
}
else if(post[i]=='*')
{
t=t1*t2;
}
else if(post[i]=='/')
{
t=t2/t1;
}
else if(post[i]=='%')
{
t=t2%t1;
}
push(t);
}
}
printf("the result is %d ",pop());
}
void main()
{
char exp[100];
printf("enter an expression");
gets(exp);
eval(exp);
}
- Implimentation of Stack using Linklist
# include <stdio.h>
# include <stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *push(struct node *p, int value)
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("No Memory available\n");
exit(0);
}
temp->data = value;
temp->next = p;
p = temp;
return(p);
}
void main()
{
struct node *top = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be pushed\n");
scanf("%d",&value);
top = push(top,value);
printf("Enter 1 to continue 0 to break\n");
scanf("%d",&n);
} while(n == 1);
if(temp==NULL)
{
printf("No Memory available \n");
exit(0);
}
temp->data = value;
temp->link=NULL;
if(*rear == NULL)
{
*rear = temp;
*front = *rear;
}
else
{
(*rear)->link = temp;
*rear = temp;
}
}
void main()
{
struct node *front=NULL,*rear = NULL;
int n,value;
do
{
do
{
printf("Enter the element to be inserted\n");
scanf("%d",&value);
insert(&front,&rear,value);
printf("Enter 1 to continue\n");
scanf("%d",&n);
} while(n == 1);
void main()
{
int list[MAX], n, element;
printf("Enter the number of elements in the list max = 10\n");
scanf("%d",&n);
readlist(list,n);
printf("\nThe list before sorting is:\n");
printlist(list,n);
printf("\nEnter the element to be searched\n");
scanf("%d",&element);
bsearch(list,n,element);
}
-Order of parenthesis
#include"stdafx.h"
#define MAX 20
#define true 1
#define false 0
void push(char);
char pop();
void main()
{
char exp[MAX],temp;
int i,valid=true;
printf("Enter an algebraic expression : ");
gets_s(exp);
for(i=0;exp[i]!=0;i++)
{
if(exp[i]=='(' || exp[i]=='{' || exp[i]=='[')
push( exp[i] );
if(exp[i]==')' || exp[i]=='}' || exp[i]==']')
if(top == -1)
valid=false;
else
{
temp=pop();
if( exp[i]==')' && (temp=='{' || temp=='[') )
valid=false;
if( exp[i]=='}' && (temp=='(' || temp=='[') )
valid=false;
if( exp[i]==']' && (temp=='(' || temp=='{') )
valid=false;
}
}
if(top>=0)
valid=false;
if( valid==true )
printf("Valid expression\n");
else
printf("Invalid expression\n");
}
void push(char item)
{
if(top == (MAX-1))
printf("Stack Overflow\n");
else
{
top=top+1;
stack[top] = item;
}
}
char pop()
{
char ch;
if(top == -1)
printf("Stack Underflow\n");
else
ch=stack[top--];
return ch;
}
-Tower Of hanoi
#include <stdio.h>
#include <conio.h>
void main()
{
void hanoi(char,char,char,int);
char t1='A',t2='B',t3='C';
int n;