- #1
sukalp
- 53
- 2
i wanted to ask you that in c++it is important we should learn linked list before learning stack,queue that i wanted to know while studying data structures
sukalp said:i wanted to ask you that in c++it is important we should learn linked list before learning stack,queue that i wanted to know while studying data structures
QuantumQuest said:Agree to jedishrfu. In general, things when learning C or C++, go the other way around (first simple stacks and queues using pointers then linked lists) which is equally effective. But, the way you describe can serve well too.
jedishrfu said:I think it depends on whether the stacks and queues are implemented using linked lists or not.
Simple fixed size stacks and queues can be implemented using a couple of pointers and an array.
Linked lists allow for making more flexible variable sized stacks and queues.
yes right through array . pointers,structuresnewjerseyrunner said:The C++ is not relevant, the data structure is universal. As lon as you understand the concept, you can implement them in any language.
I doubt very many stacks are implemented with lists. You can really implement them in many different ways as long as they satisfy the criteria.
A linked list is a data structure that consists of a sequence of nodes, where each node contains a value and a pointer to the next node in the list. A stack, on the other hand, is a data structure that follows the Last In First Out (LIFO) principle, meaning the last item inserted is the first one to be removed. In C++, a linked list can be implemented using pointers and dynamic memory allocation, while a stack can be implemented using either an array or linked list.
It is important to learn about linked lists before stacks in C++ because stacks are often implemented using linked lists. Understanding how linked lists work will give you a better understanding of how stacks are implemented and how to use them effectively.
To create a linked list in C++, you first need to define a structure that represents a node in the list. This structure should contain a data variable to hold the value and a pointer variable to point to the next node. Then, you can use the "new" keyword to dynamically allocate memory for each new node and link them together using the pointer variable. To manipulate a linked list, you can use various operations such as inserting, deleting, and searching for nodes.
One advantage of linked lists over arrays is that linked lists can dynamically grow and shrink in size, while arrays have a fixed size. This makes linked lists more flexible and efficient for managing large amounts of data. Additionally, linked lists allow for efficient insertion and deletion of elements, which can be time-consuming and difficult with arrays.
One drawback of linked lists is that they use more memory compared to arrays. Linked lists require additional memory to store the pointers that link each node together. This can be a concern when dealing with large data sets. Additionally, accessing elements in a linked list can be slower compared to arrays, as elements cannot be accessed directly and must be traversed from the beginning of the list.