0% found this document useful (0 votes)
40 views18 pages

Ds - All Codes

Uploaded by

saykulkarni69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
40 views18 pages

Ds - All Codes

Uploaded by

saykulkarni69
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 18

1.

Infix to postfix

//C Program for Infix to Postfix Conversion.

/***********************

C Program for, Here we covert the infix expression to postfix expression by using stack, for example
a*b-c/d is the infix expression, and equivalent postfix expression is: ab*cd/-.

**********************/

#include<stdio.h>

#include<conio.h>

#define SIZ 50 /* Size of Stack */

#include <ctype.h>

int top = -1; /* Global declarations */

char s[50];

void push(char elem)

/* Function for PUSH operation */

s[++top] = elem;

char pop()

/* Function for POP operation */

return (s[top--]);

int pr(char elem)

/* Function for precedence */

switch (elem)
{

case '#':

return 0;

case '(':

return 1;

case '+':

case '-':

return 2;

case '*':

case '/':

return 3;

int main()

/* Main Program */

char infx[50], pofx[50], ch, elem;

int i = 0, k = 0;

printf("\n\nRead the Infix Expression ? ");

scanf("%s",infx);

push('#');

while ((ch = infx[i++]) != '\0')

if (ch == '(')

push(ch);

else if (isalnum(ch))

pofx[k++] = ch;

else if (ch == ')')


{

while (s[top] != '(')

pofx[k++] = pop();

elem = pop(); /* Remove ( */

else

/* Operator */

while (pr(s[top]) >= pr(ch))

pofx[k++] = pop();

push(ch);

while (s[top] != '#') /* Pop from stack till empty */

pofx[k++] = pop();

pofx[k] = '\0'; /* Make pofx as valid string */

printf("\n\nGiven Infix Expn: %s Postfix Expn: %s\n", infx, pofx);

getch();

return(0);

algorithm-

1)Examine the next element in the input.

2) If it is operand, output it.


3) If it is opening parenthesis, push it on stack.

4) If it is an operator, then

i) If stack is empty, push operator on stack.

ii) If the top of stack is opening parenthesis, push operator on stack

iii) If it has higher priority than the top of stack, push operator on stack.

iv) Else pop the operator from the stack and output it, repeat step 4

5) If it is a closing parenthesis, pop operators from stack and output them until an opening
parenthesis is encountered. pop and discard the opening parenthesis.

6) If there is more input go to step 1

7) If there is no more input, pop the remaining operators to output.

2. Stack implementation using linked list

#include <stdio.h>

#include <stdlib.h>

struct Node

int Data;

struct Node *next;

}*top;

void popStack()

struct Node *var=top;

if (top==NULL)

printf("\nStack Empty");
}

else

top = top->next;

free(var);

void push(int value)

struct Node *temp;

temp=(struct Node *)malloc(sizeof(struct Node));

temp->Data=value;

if (top == NULL)

top=temp;

top->next=NULL;

else

temp->next=top;

top=temp;

void display()

struct Node *var=top;


if(var!=NULL)

printf("\nElements are as:\n");

while(var!=NULL)

printf("\t%d\n",var->Data);

var=var->next;

printf("\n");

else

printf("\nStack is Empty");

int main()

int i=0;

top=NULL;

printf(" \n1. Push to stack");

printf(" \n2. Pop from Stack");

printf(" \n3. Display data of Stack");

printf(" \n4. Exit\n");

while(1)

printf(" \nChoose Option: ");

scanf("%d",&i);

switch(i)

{
case 1:

int value;

printf("\nEnter a value to be to pushed into Stack: ");

scanf("%d",&value);

push(value);

display();

break;

case 2:

popStack();

display();

break;

case 3:

display();

break;

case 4:

struct Node *temp;

while(top!=NULL)

temp = top->next;

free(top);
top=temp;

exit(0);

default:

printf("\nwrong choice for operation");

3. Implementation of a stack

#include<stdio.h>

#include<stdlib.h>

#define MAX 12

int s[MAX];

int top =-1;

/* function declaration*/

int isFull();

int isEmpty();

void push(); // declaration of function push

void pop();

void peek();
int main()

int choice =0;

do

printf("\n1. PUSH\n");

printf("\n2.POP\n");

printf("\n3. PEEK\n");

printf("\n4.Exit\n");

printf("\nEnter your choice\n");

scanf("%d",&choice);

switch(choice)

case 1:

push();

break;

case 2:

pop();

break;

case 3:

peek();

break;

case 4:

exit(0);

break;

}
}while(choice!=5);

return 0;

int isFull()

if(top==MAX-1)

return 1;

else

return 0;

int isEmpty()

if (top==-1)

return 1;

else

{
return 0;

void push()

if (isFull())

printf("\n Stack Overflow\n");

return;

else

int x;

printf("\nEnter a value\n");

scanf("%d",&x);

top++; // top=top+1

s[top]=x;

printf("%d value inserted at top %d",x,top);

void pop()

if ( isEmpty())

printf("\nUnderflow\n");

return;

else

{
printf("\n%d popped from stack\n",s[top]);

top--; // top=top-1;

void peek()

if ( isEmpty())

printf("\nUnderflow\n");

return;

else

printf("\n%d peeked from stack\n",s[top]);

// top--; // top=top-1;

4. Circular queue using array

// Program for Implementing Circular Queue using Array

#include<stdio.h>

#include<conio.h>

#include<stdlib.h>

#define MAX 5

void insert();

void delete();

void frontt();

int queue[MAX],rear=-1,front=-1,item;

int main()
{

int ch;

do

printf("\n\n1.Insert\n2 Delete 3. display\n4.Exit\n\n");

printf("Enter your choice\n");

scanf("%d",&ch);

switch(ch)

case 1:

insert(); break;

case 2:

delete(); break;

case 3:

frontt();

break;

case 4:

exit(0); break;

default:

printf("Invalid choice, Please try again\n");

} // end of switch

}while(1);

getch();

void insert()

if(front == (rear + 1) % MAX ) // condition for Queue Full

printf("Circular Queue is Full\n");


return;

else if ( (front==-1) && ( rear==-1))

front=rear=0;

return;

else

rear = (rear + 1) % MAX;

printf("\n\n Enter item\n");

scanf("%d",&item);

queue[rear]=item;

} // end insert

void delete()

if ((front==-1)&&(rear==-1)) // Queue is empty

printf( "\nQueue is empty\n");

return;

else if(front==rear)// it that the last element ?

{ item = queue[front];

front = rear = -1 ;

return;

else // not the last element

item = queue[front];

front = (front + 1 ) % MAX;


}

printf("\n\n Item deleted: %d ",item);

} // end of delet function

void frontt()

printf("%d\t",queue[front]);

getch();

5. Decimal to binary

#include <stdio.h>

#include <conio.h>

#define MAX 100

//declaring stack using structure

typedef struct stack

int data[MAX];

int top;

}stack;

//checking whether the stack is empty or not

int empty(stack *s)

if(s -> top == -1)

return(1);

return(0);

//checking whether the stack is full or not

int full(stack *s)

if(s -> top == MAX - 1)


return(1);

return(0);

//to push the remainder into the stack

void push(stack *s, int x)

s -> top = s -> top + 1;

s -> data[s -> top] = x;

//to pop the remainder out from the stack

int pop(stack *s)

int x;

x = s -> data[s -> top];

s -> top = s -> top - 1;

return(x);

int main()

stack s; //structure member variable

int num;

s.top = -1; //top pointer initialized with -1

printf("Enter a decimal number:");

scanf("%d", &num);

while((num != 0))

if(!full(&s)) //this condition executes if the function 'full' returns 0

push(&s, num % 2); //pushing the remainder into the stack using 'push' function

num = num / 2;

}
else

printf("Stack overflow"); //if stack reaches the MAX value

exit(0);

while(!empty(&s))

num = pop(&s); //poping the remainder out from the stack using 'pop' function

printf("%d", num);

6. Reverse a string using stack

*create an empty stack

*one by one push all characters of string to stack

*one by one pop all characters from stack and put them back to string. #include <stdio.h>

#include <string.h>

#define max 100 int top,stack[max];

void push(char x){

// Push(Inserting Element in stack) operation if(top == max-1){

printf("stack overflow");

} else {

stack[++top]=x;

void pop(){

// Pop (Removing element from stack) printf("%c",stack[top--]);


}

int main()

char str[]="DBIT KURLA"; int len = strlen(str);

int i; for(i=0;i<len;i++)

push(str[i]); for(i=0;i<len;i++) pop();

You might also like