0% found this document useful (0 votes)
68 views11 pages

Stack Implementation in C Using Arrays

The document contains three sections detailing the implementation of stack data structures in C using arrays, linked lists, and evaluating postfix expressions. Each section includes the aim, required software, program code, sample input/output, and references. All programs successfully execute and demonstrate basic stack operations such as push, pop, and display.

Uploaded by

nagas2820
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)
68 views11 pages

Stack Implementation in C Using Arrays

The document contains three sections detailing the implementation of stack data structures in C using arrays, linked lists, and evaluating postfix expressions. Each section includes the aim, required software, program code, sample input/output, and references. All programs successfully execute and demonstrate basic stack operations such as push, pop, and display.

Uploaded by

nagas2820
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

Stack Using Arrays

Stack Using Arrays


[Link]:
Date:
[Link]:
Implement a program in C to perform Stack using arrays.
[Link] Required:
TURBO C
[Link]:
#include<stdio.h>
#define MAX 10
int s[MAX];
int top=-1;
main()
{

int ch; while(1)


{
printf("\[Link] \[Link] \n [Link] \n [Link]");
printf("enter ur choice");
scanf("%d",&ch);
switch(ch)
{
case 1: push();
break;
case 2: pop();
break;
case 3: display();
break;
case 4: exit(0);
default:printf("wrong choice");
Stack Using Arrays

}
}
}
push()
{
int val;
printf("enter the value");
scanf("%d",&val);
if(top==MAX-1)
{
printf("stack is OVER flow");
}
else
{
top++; s[top]=val;
}
}
pop()
{
int val;
if(top==-1)
{
printf("under flow");
}
else
{
val=s[top]; top--;
printf("%d",val);
}
}
Stack Using Arrays

display()
{
int i;
for(i=top;i>=0;i--)
{
printf("%d",s[i]);
}
}
[Link] Input and Output:
[Link]
[Link]
[Link]
[Link] ur choice1
enter the value45
[Link]
[Link]
[Link]
[Link] ur choice3
45
[Link]
[Link]
[Link]
[Link]
enter ur choice:4
[Link]:
Program Successfully Executed.
[Link]:
[Link]
tutorials/
Stack Using Linked List

Stack Using Linked List


[Link]:
Date:
[Link]:
Implement a program in C to perform Stack using Linked List.
[Link] Required:
TURBO C
[Link]:
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node *next;
};
struct node *top=NULL,*newnode,*tmp;
int main()
{
int ch;
while(1)
{
printf("\[Link] \[Link]\[Link] \[Link]");
printf("enter ur choice");
scanf("%d",&ch);
}
switch(ch)
{
case 1: push();
break;
case 2: pop();
Stack Using Linked List

break;
case 3: display();
break;
case 4: exit(0);
default:printf("wrong choice");
}
}
push()
{
int val;
printf("enter ur value");
scanf("%d",&val);
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=val;
newnode->next=top;
top=newnode;
}
pop()
{
tmp=top;
if(tmp==NULL)
{
printf("stack is underflow");
}
else
{
printf("%d",top->data);
top=top->next;
free(tmp);
}
Stack Using Linked List

}
display()
{
if(top==NULL)
{
printf("stack is underflow");
}
else
{
tmp=top;
while(tmp!=NULL)
{
printf("%d",tmp->data);
tmp=tmp->next;
}
}
}
[Link] Input and Output:
[Link]
[Link]
[Link]
[Link] ur choice1
Enter ur value:45
[Link]
[Link]
[Link]
[Link] ur choice3
45
Stack Using Linked List

[Link]
[Link]
[Link]
[Link] ur choice4
[Link]:
Program Successfully Executed.
[Link]:
[Link]
Stack Using Linked List

Postfix Expression
[Link]:
Date:
[Link]:
Implement a C program that uses stack operations to evaluate postfix Expression.
[Link] Required:
TURBO C
[Link]:
#include <ctype.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Stack
{
int top;
unsigned capacity; int* array;
};
struct Stack* createStack(unsigned capacity)
{
struct Stack* stack= (struct Stack*)malloc(sizeof(struct Stack));
if (!stack)
return NULL;
stack->top = -1;
stack->capacity = capacity;
stack->array= (int*)malloc(stack->capacity * sizeof(int));
if (!stack->array)
return NULL;
return stack;
}
int isEmpty(struct Stack* stack)
Stack Using Linked List

{
return stack->top == -1;
}
char peek(struct Stack* stack)
{
return stack->array[stack->top];
}
char pop(struct Stack* stack)
{
if (!isEmpty(stack))
return stack->array[stack->top--];
return '$';
}
void push(struct Stack* stack, char op)
{
stack->array[++stack->top] = op;
}
int evaluatePostfix(char* exp)
{
struct Stack* stack = createStack(strlen(exp));
int i;
if (!stack)
return -1;
for (i = 0; exp[i]; ++i)
{
if (isdigit(exp[i]))
push(stack, exp[i] - '0');
else
{
int val1 = pop(stack);
Stack Using Linked List

int val2 = pop(stack);


switch (exp[i])
{
case '+':
push(stack, val2 + val1);
break;
case '-':
push(stack, val2 - val1);
break;
case '*':
push(stack, val2 * val1);
break;
case '/':
push(stack, val2 / val1);
break;
}
}
}
return pop(stack);
}
int main()
{
char exp[] = "231*+9-";
printf("postfix evaluation: %d", evaluatePostfix(exp));
return 0;
}
Stack Using Linked List

[Link] Input and Output:


postfix evaluation: -4
[Link]:
Program Successfully Executed.
[Link]:
[Link]

You might also like