22 Aug Write a menu driven program to allow the user to 1) Push, 2) Pop, 3) Print, and 4) Quit. You must use separate compilation with files main.c, boolean.h, stack, h, and stack.cst
Write a menu driven program to allow the user to 1) Push, 2) Pop, 3) Print, and 4) Quit.
You must use separate compilation with files main.c, boolean.h, stack, h, and stack.c
Here is the tes tca s e for the stack progr a m.
Run the progr a m Quit
Run the progr a m Pop – you sho ul d see an “emp ty” mes s age List – you sho ul d see an “empty” mes s a ge Push a 1 Push a 2 Push a 3 Push a 4 Push a 5 List – you sho ul d see the nu m b e r s 5, 4, 3, 2, 1 in that order Pop – mes s age indicating “5 was rem ove d” List – you sho ul d see the nu m b e r 4, 3, 2, 1 in that order Push a 6 List – 6, 4, 3, 2, 1 in tha t order Pop – “6 was remove d” Contin ue pop pi ng until you have remove d 4, 3, 2, and 1 Enter an invalid men u option to get the error mes s age Pop – “empty” mes s a ge shoul d appea r List – “empty” mes s a ge shoul d appea r pus h a 10 pop – 10 is remove d quit
,
Stack A collection of data long with operations that access that data on a LIFO (last-in, first-out) basis.
Defining types:
a single item in the stack consists of the data item and a pointer to the next stacknode structure
typedef struct stacknode { int data; struct stacknode *next; } *stack;
a boolean type is either true (1) or false (0)
typedef int boolean; #define TRUE 1 #define FALSE 0
Subroutines are required for the following:
Note: I'm assuming you have declared a variable in the main program using stack top; where top is a pointer to the top of the stack.
1. Initializing the stack Uses pass by reference to set the pointer to the top of the stack to NULL.
Prototype: void init_stack(stack *);
Call it using: init_stack(&top);
void init_stack(stack *s) { (*s) = NULL; }
2. Checking to see if stack is full Checks to see if enough memory is available for another stacknode structure. We try to allocate memory the size of a stacknode structure. If successful, temp will be pointing to that memory which now needs to be freed. If not, temp will be equal to NULL. Returns TRUE or FALSE
Prototype: boolean is_full(void);
Call it using: if (is_full()) OR if (!is_full())
boolean is_full(void) { stack temp; temp = (stack) malloc (sizeof(struct stacknode)); if (temp == NULL) return TRUE; else { free (temp); return FALSE; } }
3. Checking to see if stack is empty Receives stack via pass by value and determines whether or not pointer to the top of the stack is pointing to NULL. Returns TRUE or FALSE
Prototype: boolean is_empty(stack);
Call it using: if (is_empty(top)) OR if (!is_empty(top))
boolean is_empty(stack s) { if (s == NULL) return TRUE; else return FALSE;
}
4. Pushing new items on the stack You must make sure stack is not full before calling this subroutine. Receives stack via pass by reference and data to be added via pass by value Allocates memory for new stacknode structure and assigns it to temp sets the data portion equal to the item to be pushed sets the next pointer to point to the top of the stack (ie top stacknode structure) moves the top pointer to this new stacknode structure
Prototype: void push(stack *, int);
Call it using: push (&top, data_item);
void push(stack *s, int x) { stack temp; temp = (stack) malloc(sizeof(struct stacknode)); temp -> data = x; temp -> next = (*s); (*s) = temp; }
5. Popping items off the stack You must make sure the stack is not empty before calling this subroutine. Receives stack via pass by reference Uses a temporary stack called temp. set temporary stack pointer to the top of the stack set data to be popped equal to temp's data move top of stack to stacknode structure pointed to by temp's next pointer free temp (ie free memory used for stacknode structure temp is pointing to)
Prototype: int pop(stack *);
Call it using: something = pop(&top);
int pop(stack *s) { stack temp; int data_popped; temp = *s; data_popped = temp->data; *s = temp->next; free (temp); return data_popped; }
6. Listing contents of the stack (without dumping them) You must make sure stack is not empty prior to calling this subroutine.
This subroutine will receive the stack via pass by value and list the contents without altering them. NOTE: This subroutine using recursion (calls upon itself) to print the contents of all stacknodes.
Call it using: print_stack(top);
void print_stack(stack s) { if (!is_empty(s)){ printf(“%dn”, s->data); print_stack(s->next); } }
Our website has a team of professional writers who can help you write any of your homework. They will write your papers from scratch. We also have a team of editors just to make sure all papers are of HIGH QUALITY & PLAGIARISM FREE. To make an Order you only need to click Ask A Question and we will direct you to our Order Page at WriteDemy. Then fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Fill in all the assignment paper details that are required in the order form with the standard information being the page count, deadline, academic level and type of paper. It is advisable to have this information at hand so that you can quickly fill in the necessary information needed in the form for the essay writer to be immediately assigned to your writing project. Make payment for the custom essay order to enable us to assign a suitable writer to your order. Payments are made through Paypal on a secured billing page. Finally, sit back and relax.
About Wridemy
We are a professional paper writing website. If you have searched a question and bumped into our website just know you are in the right place to get help in your coursework. We offer HIGH QUALITY & PLAGIARISM FREE Papers.
How It Works
To make an Order you only need to click on “Order Now” and we will direct you to our Order Page. Fill Our Order Form with all your assignment instructions. Select your deadline and pay for your paper. You will get it few hours before your set deadline.
Are there Discounts?
All new clients are eligible for 20% off in their first Order. Our payment method is safe and secure.
