answersLogoWhite

0


Best Answer

#include<iostream>

#include<time.h>

#include<forward_list>

int main()

{

// seed random number generator

srand ((unsigned) time(NULL));

// a forward_list is a singly-linked list

std::forward_list<int> stack;

// push 10 integers onto stack

for (int loop=0; loop<10; ++loop)

{

int num=rand();

std::cout<<"Pushing "<<num<<std::endl;

stack.push_front (num);

}

// pop all integers

while (!stack.empty())

{

std::cout<<"Popping "<<stack.front()<<std::endl;

stack.pop_front();

}

}

User Avatar

Wiki User

10y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: What is the C plus plus programs to implement the stack ADT using a singly linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

What is the front of the stack in a stacked linked list?

The top of a stack implemented as a linked list is the head of the list. All insertions and extractions occur at the head thus a forward list (singly-linked list) is sufficient to implement a stack.


A stack can be implemented as?

One way to implement a stack is with a singly-linked list. The head pointer always points to the "top of the stack", and each element points to the next. Since you rarely, if ever, are interested in more than the top of the stack, this is one way, although not the most efficient way, to implement a dynamic stack.Since a stack is an abstraction, the best way to do this is to use a C++ abstract class or JAVA interface, as that would allow you to fine tune the implementation without having to think about the utilization. Since since the category did not specify C++ or JAVA, however, that ends that discussion.


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.


Explain The merits of using a deque to implement a stack in data structure?

Explain The merits of using a deque to implement a stack in data structure


Why stack is implemented by link list?

A Linked-List is basically a series of Nodes. Each Node contains two things: The contents, and the pointer to the next Node in the Linked-List. So you can traverse the Linked-List by following the "next" pointers in each Node, a bit like following road directions from city to city. A stack is an abstract data type where you have two operations: "push" and "pop". Pushing means to put an item in the stack, Popping means to get the first element of the stack. When you push an item onto a stack, you put the item at the top: so its like cutting in line to the very front. The last one in is now first, and thus, the first one out. Another helpful image is a "stack" of trays at a cafeteria -- you can only get the tray from the top of the stack, or put a tray on top of the stack. The very first tray in the stack is actually the one at the very bottom, and thus, the last one to be used. "First in, Last Out." A stack deals with what comes first/last, while a Linked-List describes how data is stored. A stack needs to store data, so a stack can be implemented as a Linked-List.

Related questions

What is the front of the stack in a stacked linked list?

The top of a stack implemented as a linked list is the head of the list. All insertions and extractions occur at the head thus a forward list (singly-linked list) is sufficient to implement a stack.


Is it possible to implement stack and queues using linkes list?

Yes it is possible to implement stack and queue using linked list


A stack can be implemented as?

One way to implement a stack is with a singly-linked list. The head pointer always points to the "top of the stack", and each element points to the next. Since you rarely, if ever, are interested in more than the top of the stack, this is one way, although not the most efficient way, to implement a dynamic stack.Since a stack is an abstraction, the best way to do this is to use a C++ abstract class or JAVA interface, as that would allow you to fine tune the implementation without having to think about the utilization. Since since the category did not specify C++ or JAVA, however, that ends that discussion.


How do you implement stack without array?

Stacks are often implemented using the same node structure as a linked list.


What are advantages of stack?

some disadvantages created in stack using array then that problem solve to linked list use in stack.First advantage for size of stack not limited in linked list using.second essay to stack programme implement using only one pointer.


Explain The merits of using a deque to implement a stack in data structure?

Explain The merits of using a deque to implement a stack in data structure


What is the pseudo code for returning the top element of a stack?

A stack is a singly-linked list where new elements are added (pushed) to the head, and existing elements are removed (popped) from the head. As such, the stack object need only maintain a pointer to the head node and implement the pop and push methods. A stripped-down implementation in C++ follows: // Forward declaration class stack; // Minimal declaration... class node { friend node * stack::pop(); node * next; }; // Minimal implementation of stack demonstrating pop implementation class stack { node * head; public: node * pop() { // store the head pointer node * popped = head; // update head pointer if not NULL if( head ) head = head.next; // return stored pointer return( popped ); } };


Can heap implement recursion?

Heap is a data-structure, it cannot implement anything. On the other hand, it is true that: 1. Recursive routines might use heap. 2. You can use dynamic memory allocation (heap), to implement a stack; and use the stack to implement recursion.


What is the importance of stack algorithm in your program?

Stack implementations allow us to easily implement backtracking algorithms.


What are application of stack?

Stacks are primarily used to implement backtracking algorithms.


C program to implement tower of hanoi using array implementation of stack abstract datatype?

stack abstract datatype


Why stack is implemented by link list?

A Linked-List is basically a series of Nodes. Each Node contains two things: The contents, and the pointer to the next Node in the Linked-List. So you can traverse the Linked-List by following the "next" pointers in each Node, a bit like following road directions from city to city. A stack is an abstract data type where you have two operations: "push" and "pop". Pushing means to put an item in the stack, Popping means to get the first element of the stack. When you push an item onto a stack, you put the item at the top: so its like cutting in line to the very front. The last one in is now first, and thus, the first one out. Another helpful image is a "stack" of trays at a cafeteria -- you can only get the tray from the top of the stack, or put a tray on top of the stack. The very first tray in the stack is actually the one at the very bottom, and thus, the last one to be used. "First in, Last Out." A stack deals with what comes first/last, while a Linked-List describes how data is stored. A stack needs to store data, so a stack can be implemented as a Linked-List.