Question

Using C

» Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should implement the following functions t

Please comment

0 0
Add a comment Improve this question Transcribed image text
Answer #1

https://TartOldf O saved main.c E 132 Stack; 133 Stack *createstack() { 134 135 clang version > clang-7 -pt Stack *s malloc (

#include <stdio.h>
#include <stdlib.h>

// struct for node..
// note: Node is a component of list
typedef struct Node {
        int data; 
        struct Node *next;
} Node;

// struct for list
// Note: a list contains multiple nodes
typedef struct List {
        Node *head;
        int size;
} List;

// create a node of list
Node *createNode() {
        return malloc(sizeof(Node));
}

// create a new list dynamically
List *createList() {
        List *result = malloc(sizeof(List));
        // list has no node in start, so head is null
        result->head = NULL;
        result->size = 0;
        return result;
}

// get the front node of the list
Node *front(List *list) {
        return list->head;
}

// Insert a new value to the list at given index
// Return the reference to new node.
Node *insert(List *list, int index, int data) {

        // get the first node of list
        Node *start = list->head;

        // create the new node to be inserted in the list
        Node *x = createNode();
        x->data = data;

        // check if new node need to be put at the head
        if(index == 0) {
                x->next = start;

                // if we put new node at start, it means original
                // head of list get changed.
                list->head = x;
                list->size += 1;
        } else {
                // Else, the new node will be in betwene the list

                // Try to find a node, after which we will insert
                Node *prev = start;
                while(prev != NULL && index != 1) {
                        prev = prev->next;
                        index--;
                }

                // We need to insert after prev node, if index 
                // was valid

                if(prev != NULL) {
                        list->size += 1;
                        x->next = prev->next;
                        prev->next = x;
                        return x;
                }
        }

        // invalid index.
        return NULL;
}

// this method removes the node on a given index from
// the list, returns null, if node not found.
int removeElem(List *list, int index) {
        Node *start = list->head;

        // if head need to be removed.
        if(index == 0) {
                if(start == NULL) {
                        return -1;
                }
                // the original head gets changed
                list->head = start->next;
                list->size -= 1;
                return start->data;
        } else {
                // We are trying to find a prev node, after which
                // we will remove the node.
                Node *prev = start;
                while(prev != NULL && index != 1) {
                        prev = prev->next;
                        index--;
                }

                // if such node exists, 
                if(prev != NULL) {
                        if(prev->next != NULL) {
                                // remove the node after prev.
                                list->size -= 1;
                                int data = prev->next->data;
                                prev->next = prev->next->next;
                                return data;
                        }
                }
        }

        // invalid index.
        return -1;
}

// this method prints the list starting from head.
void printList(List *list) {
        Node *start = list->head;
        while(start != NULL) {
                printf("%d ", start->data);
                start = start->next;
        }
        printf("\n");
}

////////////////////// STACK WRAPPERS //////////////////////////////
typedef struct Stack {
        List *list;
} Stack;
Stack *createStack() {
        Stack *s = malloc(sizeof(Stack));
        s->list = createList();
        return s;
}
void push(Stack *stack, int value) {
        insert(stack->list, 0, value);
}
int pop(Stack *stack) {
        return removeElem(stack->list, 0);
}

////////////////////// QUEUE WRAPPERS //////////////////////////////
typedef struct Queue {
        List *list;
} Queue;
Queue *createQueue() {
        Queue *s = malloc(sizeof(Queue));
        s->list = createList();
        return s;
}
void enqueue(Queue *queue, int value) {
        insert(queue->list, queue->list->size, value);
}
int dequeue(Queue *queue) {
        return removeElem(queue->list, 0);
}

int main(void) {
        List *list = createList();
        insert(list, 0, 10);
        insert(list, 1, 30);
        insert(list, 0, 20);
        insert(list, 2, 40);

        printList(list);

        removeElem(list, 0);
        removeElem(list, 2);

        printList(list);

        Stack *stack = createStack();
        push(stack, 1);
        push(stack, 2);
        push(stack, 3);
        push(stack, 4);
        printf("%d ", pop(stack));
        printf("%d ", pop(stack));
        printf("%d ", pop(stack));
        printf("%d\n", pop(stack));

        Queue *queue = createQueue();
        enqueue(queue, 1);
        enqueue(queue, 2);
        enqueue(queue, 3);
        enqueue(queue, 4);
        printf("%d ", dequeue(queue));
        printf("%d ", dequeue(queue));
        printf("%d ", dequeue(queue));
        printf("%d\n", dequeue(queue));
}


   Please upvote, as i have given the exact answer as asked in question. Still in case of any issues in code, let me know in comments. Thanks!

Add a comment
Know the answer?
Add Answer to:
» Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT