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
  • Please write a Java interface for an integer stack (should have the methods push, pop, toString)....

    Please write a Java interface for an integer stack (should have the methods push, pop, toString). Then implement this interface using one of our linked list nodes. Then please write an interface for an integer queue ( should have methods enqueue, dequeue, and toString). Then implement this interface using one of our linked list objects. Please see chapter 3 in the text for a definition of a stack. Please write a program that asks the user for how many numbers...

  • Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a...

    Create a flowchart to represent the Push and Pop operations for a Stack based on a linked list data structure. Create a flowchart to represent the Enqueue and Dequeue operations for a Queue based on a linked list data structure. Write the required Java code to implement either a Stack or a Queue data structure based on a linked list. The code should include the class constructors, the necessary properties, and methods to add and remove elements from the data...

  • I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (ont...

    I am to implement a simple simulation that supports a stack and a queue of items being either enqueued and dequeued (onto the queue) or pushed and popped (onto the stack). I are required to use STL data structures to implement and create the stack and queue for my program. ----- testfile1.tst -------- enqueue 5 enqueue 7 push blooper push rookie dequeue push demerits pop enqueue 3 enqueue 8 push showplace enqueue 9 dequeue pop dequeue push palmetto push zebra...

  • Write a C++ program to implement Queue ADT using singly linked structure. The program includes the...

    Write a C++ program to implement Queue ADT using singly linked structure. The program includes the following: Define the Queue class template in the header file QueueLinked.h. // QueueLinked.h #ifndef QUEUE_H #define QUEUE_H #include <iostream> using namespace std; template <typename T> class Queue { public: // Constructor Queue(); //Desctructor ~Queue(); // Makes the queue to the empty state. void make_empty(); // Checks if the queue is empty. bool empty() const; // Inserts item at the end of the queue. void...

  • HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE...

    HI USING C++ CAN YOU PLEASE PROGRAM THIS ASSIGNMENT AND ADD COMMENTS: stackARRAY: #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the top element from Stack int topElement(); // get the top element void display(); // display Stack elements from top to bottom }; void Stack...

  • Please answer in C++. Derive a class called Stack from the linked list described in Assignment...

    Please answer in C++. Derive a class called Stack from the linked list described in Assignment 2 (list of Dates). This means the Stack class will inherit all the properties (data and functions) of the linked list. But, since a stack only allows pushing and popping at the front of the list only, you will need to prevent the operations at the back. To do this, derive the Stack class in such a way that the base class (LinkedList) functions...

  • Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions...

    Question1 ) The STL provides similiar classes called stack and queue, both of which gave functions called push and pop. Answer the following questions: 1.1) Does the push perform a different action in the stack class then in the queue class? Explain the answer. 1.2) Does the pop perform a different action in the stack class then in the queue class? Explain the answer. Question 2) 2.1) Suppose you are tasked with implementing a reverse queue in which elements are...

  • I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas....

    I RE: Singly Linked List, Stack, and Queue Implementation Suppose, you have the following Node clas. public class Node ! int id; Node next: public Node (int id) ( this.id id: Write program codes for the traditional: 1) append int id), prepend(int id), removeFirstNodeO. displayAlINodesO, and findById(int id) operations for a singly linked list 2) pushint id), pop), peek0, displayAllNodes0 operations for a stack 3) enQueue(int id), deQueuel), displayAINodes() operations for a gueue Please make sure that you declare separate...

  • In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and...

    In C++ Implement a queue data structure using two stacks. Remember a queue has enqueue and dequeue functions. You could use either the array or linked list implementation for stacks and queues. Source for stack array: --------------------------------------------------- #include<iostream> #define SIZE 100 #define NO_ELEMENT -999999 using namespace std; class Stack { int arr[SIZE]; // array to store Stack elements int top; public: Stack() { top = -1; } void push(int); // push an element into Stack int pop(); // pop the...

  • PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite...

    PART A - STACKS To complete this assignment: Please study the code posted below. Please rewrite the code implementing a template class using a linked list instead of an array. Note: The functionality should remain the same *************************************************************************************************************** /** * Stack implementation using array in C/procedural language. */ #include <iostream> #include <cstdio> #include <cstdlib> //#include <climits> // For INT_MIN #define SIZE 100 using namespace std; /// Create a stack with capacity of 100 elements int stack[SIZE]; /// Initially stack is...

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