a) Write a function that gets a stack of ints and returns the number of elements in it. When the function returns, the stacks must be in their initial state. // returns the size of the stack int stack_size(stack_t* s) b) Write a function that gets two stacks of ints and checks if they are equal (i.e., have the same elements in the same order). When the function returns, the stacks must be in their initial state. // checks if the two stacks are equal bool stack_equal(stack_t* s1, stack_t* s2) c) Write a function that gets a stack of chars and returns the string consisting of the chars in it. When the function returns, the stack must be in its initial state. // converts stack to a string // For example, suppose we push 'A', then 'B', and then 'C'. The function needs to return the string "ABC". char* stack_to_string(stack_t* s) ** Remember: you should only use the provided interface, and not assume that stack_t is implemented in a certain way. /* Question 1 */ // returns the size of the stack int stack_size(stack_t* s); // checks if the two stacks are equal bool stack_equal(stack_t* s1, stack_t* s2); // converts stack to a string // For example, suppose we push 'A', then 'B', and then 'C'. // The function needs to return the string "ABC". char* stack_to_string(stack_t* s);

Database System Concepts
7th Edition
ISBN:9780078022159
Author:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Chapter1: Introduction
Section: Chapter Questions
Problem 1PE
icon
Related questions
Question

we get a stack of chars. The implementation is given in a separate file. You should not make assumptions about the exact implementation details. You may only use the following functions to access the stack.

typedef struct {

// not known

} stack_t;

// creates a new stack

stack_t* stack_create();

// pushes a given item to the stack

void stack_push(stack_t* s, char item);

// pops the top element from the stack

// Pre condition: stack is not empty

char stack_pop(stack_t* s);

// checks if the stack is empty

bool stack_is_empty(stack_t* s);

// frees the stack

void stack_free(stack_t* s);

a) Write a function that gets a stack of ints and returns the number of elements in it. When the function returns, the stacks must be in their initial state.

// returns the size of the stack

int stack_size(stack_t* s)

b) Write a function that gets two stacks of ints and checks if they are equal (i.e., have the same elements in the same order). When the function returns, the stacks must be in their initial state.

// checks if the two stacks are equal

bool stack_equal(stack_t* s1, stack_t* s2)

c) Write a function that gets a stack of chars and returns the string consisting of the chars in it. When the function returns, the stack must be in its initial state.

// converts stack to a string

// For example, suppose we push 'A', then 'B', and then

'C'. The function needs to return the string "ABC".

char* stack_to_string(stack_t* s)

** Remember: you should only use the provided interface, and not assume that stack_t is implemented in a certain way.

/* Question 1 */

// returns the size of the stack
int stack_size(stack_t* s);

// checks if the two stacks are equal
bool stack_equal(stack_t* s1, stack_t* s2);

// converts stack to a string
// For example, suppose we push 'A', then 'B', and then 'C'.
// The function needs to return the string "ABC".
char* stack_to_string(stack_t* s);


test function for the question:

bool test_q1a() {

stack_t* s = stack_create();
inti=0;

for (i=0;i<100;i++) stack_push(s, i);
intsize1 = stack_size(s);

for (i=0;i<10;i++) stack_pop(s);
intsize2 = stack_size(s);

for (i=0;i<60;i++) stack_push(s, 'a');
intsize3 = stack_size(s);

stack_free(s);

if (size1==100 && size2==90 && size3==150) {
printf("Q1a ok\n");
returntrue;
}
else {
printf("Q1a ERROR\n");
returnfalse;
}
}


bool test_q1b() {

stack_t* s1 = stack_create();
stack_t* s2 = stack_create();
inti=0;

for (i=0;i<20;i++) stack_push(s1, 'a'+i);
for (i=0;i<20;i++) stack_push(s2, 'a'+i);
boolis_eq1 = stack_equal(s1, s2);

intsize = stack_size(s1);

for (i=0;i<5;i++) stack_push(s1, '0'+i);
for (i=0;i<5;i++) stack_push(s2, '0'+i);

boolis_eq2 = stack_equal(s1, s2);

stack_free(s2);
stack_free(s1);

if (is_eq1 && is_eq2 && size==20) {
printf("Q1b ok\n");
returntrue;
}
else {
printf("Q1b ERROR\n");
returnfalse;
}
}



bool test_q1c() {

charstr[] = "hello world";
stack_t* s1 = stack_create();
inti=0;
for (i=0;i<11;i++) stack_push(s1, str[i]);
char* ans = stack_to_string(s1);
boolcheck1 = ans && strcmp(ans, str)==0;
free(ans);

stack_free(s1);

if (check1) {
printf("Q1c ok\n");
returntrue;
}
else {
printf("Q1c ERROR\n");
returnfalse;
}
}
C stack.c >
3
#include <stdbool.h>
4
#include "stack.h"
stack_t* stack_create() {
stack_t* s = (stack_t*) mailoc(sizeof(stack_t));
7
8.
9.
s->head = NULL;
10
s->tail = NULL;
11
return s;
12
}
13
14
void stack_push(stack_t* s, char item) {
15
node_t* new_head =
(node_t*) malloc(sizeof(node_t));
16
new_head->data
item;
new_head->next = s->head;
S->head = new_head;
17
18
19
}
20
21
char stack_pop(stack_t* s) {
22
node_t* prev_head = s->head;
23
char ret =
prev_head->data;
24
s->head = prev_head->next;
25
free(prev_head);
26
return ret;
27
}
28
bool stack_is_empty(stack_t* s) {
NULL);
29
30
return (s->head
31
}
32
33
void stack_free(stack_t* s) {
while (!stack_is_empty(s))
34
35
stack_pop(s);
free(s);
36
37
38
}
Transcribed Image Text:C stack.c > 3 #include <stdbool.h> 4 #include "stack.h" stack_t* stack_create() { stack_t* s = (stack_t*) mailoc(sizeof(stack_t)); 7 8. 9. s->head = NULL; 10 s->tail = NULL; 11 return s; 12 } 13 14 void stack_push(stack_t* s, char item) { 15 node_t* new_head = (node_t*) malloc(sizeof(node_t)); 16 new_head->data item; new_head->next = s->head; S->head = new_head; 17 18 19 } 20 21 char stack_pop(stack_t* s) { 22 node_t* prev_head = s->head; 23 char ret = prev_head->data; 24 s->head = prev_head->next; 25 free(prev_head); 26 return ret; 27 } 28 bool stack_is_empty(stack_t* s) { NULL); 29 30 return (s->head 31 } 32 33 void stack_free(stack_t* s) { while (!stack_is_empty(s)) 34 35 stack_pop(s); free(s); 36 37 38 }
C stack.h > ...
1
#ifndef STACK_H
#define STACK_H
3
4
struct node {
char data;
struct node * next;
7
};
9.
typedef struct node node_t;
10
11
typedef struct {
node_t* head;
12
13
14
node_t* tail;
15
} stack_t;
16
17
// creates a new stack
18
stack_t* stack_create();
19
// pushes a given item to the stack
void stack_push(stack_t* s, char item);
20
21
22
// remove an item from the top of the stack
char stack_pop(stack_t* s);
23
24
25
26
// checks if stack is empty
27
bool stack_is_empty(stack_t* s);
28
// frees the stack
void stack_free(stack_t* s);
29
30
31
32
33
#endif
34
Transcribed Image Text:C stack.h > ... 1 #ifndef STACK_H #define STACK_H 3 4 struct node { char data; struct node * next; 7 }; 9. typedef struct node node_t; 10 11 typedef struct { node_t* head; 12 13 14 node_t* tail; 15 } stack_t; 16 17 // creates a new stack 18 stack_t* stack_create(); 19 // pushes a given item to the stack void stack_push(stack_t* s, char item); 20 21 22 // remove an item from the top of the stack char stack_pop(stack_t* s); 23 24 25 26 // checks if stack is empty 27 bool stack_is_empty(stack_t* s); 28 // frees the stack void stack_free(stack_t* s); 29 30 31 32 33 #endif 34
Expert Solution
trending now

Trending now

This is a popular solution!

steps

Step by step

Solved in 3 steps

Blurred answer
Knowledge Booster
Stack
Learn more about
Need a deep-dive on the concept behind this application? Look no further. Learn more about this topic, computer-science and related others by exploring similar questions and additional content below.
Similar questions
  • SEE MORE QUESTIONS
Recommended textbooks for you
Database System Concepts
Database System Concepts
Computer Science
ISBN:
9780078022159
Author:
Abraham Silberschatz Professor, Henry F. Korth, S. Sudarshan
Publisher:
McGraw-Hill Education
Starting Out with Python (4th Edition)
Starting Out with Python (4th Edition)
Computer Science
ISBN:
9780134444321
Author:
Tony Gaddis
Publisher:
PEARSON
Digital Fundamentals (11th Edition)
Digital Fundamentals (11th Edition)
Computer Science
ISBN:
9780132737968
Author:
Thomas L. Floyd
Publisher:
PEARSON
C How to Program (8th Edition)
C How to Program (8th Edition)
Computer Science
ISBN:
9780133976892
Author:
Paul J. Deitel, Harvey Deitel
Publisher:
PEARSON
Database Systems: Design, Implementation, & Manag…
Database Systems: Design, Implementation, & Manag…
Computer Science
ISBN:
9781337627900
Author:
Carlos Coronel, Steven Morris
Publisher:
Cengage Learning
Programmable Logic Controllers
Programmable Logic Controllers
Computer Science
ISBN:
9780073373843
Author:
Frank D. Petruzella
Publisher:
McGraw-Hill Education