Stack Data Structure Operations in C: Exercises and Examples, Exercises of Data Structures and Algorithms

problems and solve stack data structure

Typology: Exercises

2018/2019

Uploaded on 03/03/2019

hassan-yosri
hassan-yosri 🇪🇬

2 documents

1 / 3

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
#include<stdio.h>
#include<stdlib.h>
#include "stack.h"
void display(Stack s){
if(emptyStack(s))
printf("Stack is empty!\n");
Stack tmp = initStack();
while(!emptyStack(s)){
int x = pop(s);
printf("%d\n", x);
push(tmp, x);
}printf("\n");
while(!emptyStack(tmp)){
push(s, pop(tmp));
}
}
int length(Stack s){
int cnt = 0;
Stack tmp = initStack();
while(!emptyStack(s)){
push(tmp, pop(s));
cnt++;
}
while(!emptyStack(tmp)){
push(s, pop(tmp));
}
return cnt;
}
// 1. Get the maximum element in a stack.
int maximum(Stack s){
if(emptyStack(s)){
printf("Stack is empty!\n");
return -1;
}
int max = peek(s), x;
Stack tmp = initStack();
while(!emptyStack(s)){
x = pop(s);
if(x > max)
max = x;
push(tmp, x);
}
while(!emptyStack(tmp)){
push(s, pop(tmp));
}
return max;
}
// 2. Get the average of the elements in a stack.
double average(Stack s){
if(emptyStack(s)){
printf("Stack is empty!\n");
return -1;
}
int length = 0, x;
double sum = 0;
Stack tmp = initStack();
while(!emptyStack(s)){
x = pop(s);
push(tmp, x);
sum += x;
length++;
}
while(!emptyStack(tmp)){
push(s, pop(tmp));
}
return sum / length;
}
// 3. Check if two stacks are equal.
bool isEqual(Stack s1, Stack s2){
Stack tmp1 = initStack(), tmp2 = initStack
();
bool retEqual = true;
while(!emptyStack(s1) && !emptyStack
(s2) && retEqual){
int d1 = pop(s1), d2 = pop(s2);
push(tmp1, d1);
push(tmp2, d2);
if(d1 != d2)
retEqual = false;
}
if(!emptyStack(s1) || !emptyStack(s2))
retEqual = false;
while(!emptyStack(tmp1)){
push(s1, pop(tmp1));
push(s2, pop(tmp2));
}
return retEqual;
}
// 4. Check if two stacks are reverse to each
other.
pf3

Partial preview of the text

Download Stack Data Structure Operations in C: Exercises and Examples and more Exercises Data Structures and Algorithms in PDF only on Docsity!

#include<stdio.h>

#include<stdlib.h> #include "stack.h"

void display(Stack s){ if(emptyStack(s)) printf("Stack is empty!\n"); Stack tmp = initStack(); while(!emptyStack(s)){ int x = pop(s); printf("%d\n", x); push(tmp, x); }printf("\n"); while(!emptyStack(tmp)){ push(s, pop(tmp)); } }

int length(Stack s){ int cnt = 0; Stack tmp = initStack(); while(!emptyStack(s)){ push(tmp, pop(s)); cnt++; } while(!emptyStack(tmp)){ push(s, pop(tmp)); } return cnt; }

// 1. Get the maximum element in a stack. int maximum(Stack s){ if(emptyStack(s)){ printf("Stack is empty!\n"); return -1; } int max = peek(s), x; Stack tmp = initStack(); while(!emptyStack(s)){ x = pop(s); if(x > max) max = x; push(tmp, x); } while(!emptyStack(tmp)){ push(s, pop(tmp)); }

return max; }

// 2. Get the average of the elements in a stack. double average(Stack s){ if(emptyStack(s)){ printf("Stack is empty!\n"); return -1; } int length = 0, x; double sum = 0; Stack tmp = initStack(); while(!emptyStack(s)){ x = pop(s); push(tmp, x); sum += x; length++; } while(!emptyStack(tmp)){ push(s, pop(tmp)); } return sum / length; }

// 3. Check if two stacks are equal. bool isEqual(Stack s1, Stack s2){ Stack tmp1 = initStack(), tmp2 = initStack (); bool retEqual = true; while(!emptyStack(s1) && !emptyStack (s2) && retEqual){ int d1 = pop(s1), d2 = pop(s2); push(tmp1, d1); push(tmp2, d2); if(d1 != d2) retEqual = false; } if(!emptyStack(s1) || !emptyStack(s2)) retEqual = false; while(!emptyStack(tmp1)){ push(s1, pop(tmp1)); push(s2, pop(tmp2)); } return retEqual; }

// 4. Check if two stacks are reverse to each other.

bool isReverse(Stack s1, Stack s2){

Stack s3 = initStack(); bool retReverse = true; while(!emptyStack(s2)){ push(s3, pop(s2)); } retReverse = isEqual(s1, s3); while(!emptyStack(s3)){ push(s2, pop(s3)); } return retReverse; }

// 5. Check if the sum of the upper half of a stack is the same as the sum of the lower half. bool isUpperLower(Stack s){ if(emptyStack(s))return 0; int len = length(s), upperSum = 0, lowerSum = 0, x, i; Stack tmp = initStack(); for(i=0; i<len/2; i++){ x = pop(s); push(tmp, x); upperSum += x; } if(len % 2 != 0)push(tmp, pop(s)); for(i=0; i<len/2; i++){ x = pop(s); push(tmp, x); lowerSum += x; } while(!emptyStack(tmp)){ push(s, pop(tmp)); } return upperSum == lowerSum; }

int main(){ int n = 4, i; int a[] = {1, 2, 3, 4}; int a1[] = {4, 3, 2, 1}; Stack s = initStack(), s2 = initStack(), s3 = initStack(); for(i=0; i<n; i++){ push(s, a[i]); push(s2, a[i]); } for(i=0; i<n; i++){

push(s3, a1[i]); } printf("STACK:\n"); display(s); display(s2); display(s3); printf("MAX: %d\n", maximum(s)); printf("AVG: %.2f\n", average(s)); printf("IS_EQUAL: %s\n", (isEqual(s, s2)? "Stacks are [EQUAL]" : "Stacks are [not EQUAL]")); printf("IS_REVERSE: %s\n", (isReverse(s, s3)? "Stacks are [REVERSE] to each other" : "Stacks are [not REVERSE] to each other")); printf("IS_UPPER_EQUAL_LOWER: %s \n", (isUpperLower(s3)? "Upper half [EQUALS] lower half" : "Upper half does [not EQUAL] lower half"));

return 0; }

enum bool {true = 1, false = 0}; typedef enum bool bool;

typedef struct node{ int data; struct node *next; } Node, *NodePtr;

typedef struct stackType{ NodePtr top; } StackType, *Stack;

NodePtr makeNode(int x){ NodePtr np = (NodePtr) malloc(sizeof (Node)); np->next = NULL; np->data = x; return np;