#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!
» Part A: Stack Create a Stack struct that is a wrapper for your linked list o You should impleme...
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 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 (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...
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 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...
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 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...
e. public class Queue { // Uses the correct Stack class from ME2 Ex 19 private Stack mStack; public Queue() { setStack(new Stack()); } public Queue enqueue(E pData) { getStack().push(pData); return this; } public E dequeue() { return getStack().pop(); } public E peek() { return getStack.peek(); } private Stack getStack() { return mStack; } private void setStack(Stack pStack) { mStack = pStack; } } f. public class Queue extends Stack { // Uses the correct Stack class from ME2 Ex...
You are going to create a Queue. (alternately you can create a list and simply implement enqueue and dequeue functions in the List – that will technically make it a queue). You will fill the first list with numbers consecutively numbered from 2 to n where n is entered by the user (we will call this Q1). When creating your Queue object use the correct function names for enqueue and dequeue functions. Again – sorry, cannot use an Javascript array...
1)Given a Stack implemented with a Linked List, and an O(1) implementation of push and pop,show the Linked List after the following commands are executed: Stack myStack = new Stack(); myStack.push(20); myStack.push(40); myStack.pop(); myStack.push(60); myStack.push(80); 2)If the same commands were used but the Stack was implemented with an Array with maximum size of 10, show what the array would look like after all these commands are executed. Assume O(1) implementation of push and pop here as well. 3)Given a Queue...