Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

Demo Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we

image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
image text in transcribed
Demo Here we see a Stack ADT implemented using array. We would like the stack to be usable for different max sizes though, so we need to use dynamic memory allocation for our array as well. #include-stdio.h> #include typedef struct int data; II stack data, we assume integer for simplicity int top; II top of the stack int maxSize; I/ max size of the stack Stack; void Stacklnit(Stack' stack, int size) ( I/ this function initializes a stack for first use printf("Initializing stack to hold %d integers. .Nn",size) stack->maxSize = size stack->top 1; stack->data (int) malloc(sizeof int) size) void StackPush(Stack" stack, int data) if (stack->topstack->maxSize 1)( printf Stack already full, return return printf("Pushing %d n".data) stack->top++ stack->data[stack->top data; int StackPeek(Stack stack) I/ return what's on the top without removing Il TODO: implement int StackPop(Stack stack) ( Il return what's on the top and remove it Il TODO: implement int StackDestroy (Stack' stack) { Il free up memory used up by data II TODO: implement int main Stack myStack; Stacklnit(&myStack,8) StackPush(&myStack,3) StackPush(&myStack,4) StackPush(&myStack,6) StackPushi&myStack 1h stack->top++ stack->data[stack->top] data; int StackPeek(Stack' stack) { I/ return what's on the top without removing Il TODO: implement int StackPop(Stack stack) // return what's on the top and remove it II TODO: implement int StackDestroy(Stack" stack) { /I free up memory used up by data II TODO: implement int main) Stack myStack; Stacklnit(&myStack,8); StackPush(&myStack,3); StackPush(&myStack,4) StackPush(&myStack,6); StackPush(&myStack,1); printf("Peek: %d n",StackPeek(&myStack)) StackPush(&myStack,2); StackPush(&myStack,5); StackPush(&myStack,8); StackPush(&myStack,9); StackPush(&myStack,7); printf("Peek: %d n",StackPeek ( &myStack)) ; printf("Pop: %dn". StackPop(&myStack)) printf( "Pop: %dn", StackPop(& myStack)) printf("Pop: %dn", StackPop(&myStack)) rintf("Pop: %dn", StackPop(&myStack)); StackPush(&myStack, 11); StackPush(&myStack, 10) printf("Peek: %dn",StackPop(&myStack)); StackDestroy(&myStack); return 0 Problem 3 [Double Pointers] A double pointer or a pointer-to-pointer is basically a pointer that pointers to another pointer. In other words it store an address, which you look up, and you will find another address that points to another memory location If you go online to search for linked-list examples, you will often find double pointers. You must feel very frustrated in the last lab Below you will find a incomplete program on linked list that utilizes double pointer Please complete the function insertToStart so that we will have the right output. Do not change the main program or the method parameters and return types. You can look at createList as an example on using the double pointer. #include #include // <.-remember this typedef struct nodef int data i just generic meaningless node void createlist firstdata in func we create the first to list and put inside listhead is passed by reference as reftolisthead will be updated upon function end ntf list...n newnode="malloc(sizeof(Node));" newnode->data = firstData. newNode->next = NULL; refToListHead newNode; I/ refToListHead points to listHead printf( "address stored in refToListHead is %pin", reIToListHead) printf( "address stored inrefToListHead is %pin: refToListHead) void createList(Node "refToListHead, int firstData) ( // in this func, we create the first node to the list // and put the firstData inside the first node; // the listHead is passed in by reference as refToListHead // and will be updated upon function end rintf creating list...n); newNode malloc(sizeof(Node)) newNode->data new Node->next firstData NULL; .refToListHead = newNode: // reIToListHead points to listHead printf("address stored in refToListHead is %p n", refToListHead) printf("address stored in *refToListHead is %p n","refToListHead) void insertToStart(Node "refToListHead, int dataTolnsert) // we will insert the dataTolnsert to start of the list Node 'originalListHead originalListHead refToListHead / INSERT YOUR CODE HERE! void printList(Node listHead) ( printf printing list: ") Node *current = listHead while (current != NULL) { printf("%d ".current.-data) current current->next; printfn"): int main0 Node myHead NULL; II head of the linked list, not really MY H printf("address stored in myHead is %pin". myHead): createList(&myHead, 2018) printf( "address stored in myHead is 90p n", myHead) printList(myHead) insertToStart(&myHead, 2020) insertToStart(&myHead, 1900) printList(myHead) return 0; The output should look like this, if done correctly (the addresses will be different) address stored in myHead is (nil) creating list... address stored in refToListHead is 0x7fff736240c8 address stored in refToListHead is 0x10cf020 address stored in myHead is 0x10cf020 printing list: 2018 printing list: 1900 2020 2018

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Design And Implementation

Authors: Edward Sciore

2nd Edition

3030338355, 978-3030338350

More Books

Students also viewed these Databases questions