answersLogoWhite

0


Best Answer

Both questions really boil down to the difference between a stack and a queue, so let's deal with that first. A stack is used when you want a last-in, first-out collection (LIFO), like a stack of plates; the last plate you place on the stack is the first to come off the stack. A queue is a first-in, first out collection (FIFO); the first person in a queue is the first to be served.

Secondly, we would never use an array for a stack or a queue. Arrays must be resized dynamically, both to cater for new elements and to remove redundant elements. When catering for a new element, it may be necessary to reallocate the entire array because arrays must reside in contiguous memory. We can alleviate some of this burden by allocating more elements than we actually need, but this wastes precious memory unnecessarily. Alternatively, we can use a two-dimensional array to allocate non-contiguous blocks of memory (which is important when the array is large), but it still wastes memory unnecessarily.

Linked lists are the optimum model for both stacks and queues as the nodes need not reside in contiguous memory and memory can be allocated and deallocated on a per-node basis with minimum impact on performance and memory consumption. While it is true some additional memory is required per-node to cater for each node pointing to the next, there is no redundant memory. And while arrays do allow constant-time random access to any element, stacks and queues do not require random access.

The main feature of a stack is that we require constant time access to the head of the stack. Insertions and extractions are made only to the head of the stack, therefore we need only maintain one pointer to the head of the stack. When a new node is inserted, its next node points to the existing head of the stack (which may be NULL if the stack is empty), and then the head pointer points to the new node. When the head node is extracted, its next node (which may be NULL) becomes the new head pointer.

The main feature of a queue is that we require constant time access to both the head and the tail. Insertions occur at the tail and extractions at the head, therefore we need two pointers, one at the head node, the other at the tail node. Initially both point to NULL. When a new node is inserted and the tail is NULL, both the head and tail point to the new node, otherwise the tail node points to the new node and the new node becomes the tail. The tail node always points to NULL. Extracting the head node is the same as in a stack, except the tail node is set to NULL whenever the head is set to NULL.

To fully address the question, let's consider stacks and queues using arrays. We'll ignore the resizing issues simply by assuming that there are initially 10 unused elements, and whenever there are fewer than 5 unused elements the array will be resized by an extra 10 elements, and reduced by 10 elements whenever there are more than 15 unused elements. Thus there will always be at least 10 elements. There are better methods than this, but it will suffice for the purpose of this answer.

Since insertions at the start of the array would require us to copy all elements into the next element to make room for the insertion, all insertions will occur at the end of the array in the first unused element. Therefore we must maintain a zero-based index of the first unused element (the tail). When we insert an element at that index, the tail is simply incremented.

For the stack, extractions also occur at the tail. Therefore, if the tail is non-zero, we simply decrement the tail and extract the element at that index. If the tail is zero, the stack is empty. Otherwise the tail tells us how many elements have been used for resizing purposes. Resizing should be done after an insertion or extraction.

For the queue, extractions occur at the head, therefore we initially point at element zero. If the head and tail are different, an item exists at the head element so we extract it and increment the head. If the head and tail are the same, then there are no elements in use so both can be reset to zero. The only real complication is in working out how many unused elements there are for resizing purposes. For this we simply subtract the head from the tail to determine how many elements are in use. This should be done prior to any insertion. If a resize is required it would be prudent to shunt all used elements to the start of the array and adjust the head and tail accordingly, then perform the insertion.

As you can appreciate, a queue is more complex than a stack when using an array, but ultimately an array is more complex than a linked list for both stacks and queues.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: 1.difference between stack using arrays and queue using arrays and 2.difference between stack using linked list and queue using linked list?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Continue Learning about Engineering

To enter n nos without using arrays in c?

Use a linked-list.


What are disadvantages of a linked list over an array?

A linked list is better bcoz: 1. A linked list can be grown to any size whereas a statically allocated array is of a fixed size and hence can cause problems if you try to insert beyond that. 2. Deletion of elements is a problem in arrays. You either have to move all the elements following the deleted element forward which is a waste of time or you have to place a sentinel indicating that the element in that position is deleted which causes a waste of space. On the flip side, arrays are easier to handle because their memory allocation is done once and for all(atleast till you need more space and have to reallocate) and because there is no hassle with pointers.


Explain the linked list and multi linked list structures?

I tried my best to explain all Linked List. For Single Linked List http://www.fansonnote.com/2012/02/single-linked-list/ For Double Linked List http://www.fansonnote.com/2012/02/double-linked-list/ For Multi Linked List http://www.fansonnote.com/2012/02/multi-linked-list/ Hope it will help. Thanks.


What are the advantages of linked list over arrays?

# Linked lists do not need contiguous blocks of memory; extremely large data sets stored in an array might not be able to fit in memory. # Linked list storage does not need to be preallocated (again, due to arrays needing contiguous memory blocks). # Inserting or removing an element into a linked list requires one data update, inserting or removing an element into an array requires n (all elements after the modified index need to be shifted).


How do you overcome drawbacks of linked list?

Overcoming the "drawbacks" of a linked list requires knowing what drawback is at stack. If you need to iterate backwards as well as forwards, then you could create a doubly linked list. If you need to search for elements quickly, then you could implement a binary tree. If you have a static size, then you could implement an array. It's all a matter of tradeoff, and of what your particular issue is... Its badsector... According to my self disadvantage of link list that searching in link list is sequential if you compare it with arrays its very slow. Because in link list we have to search every node for that. if any one uses binary tree that is in some cases more faster than arrays.

Related questions

To enter n nos without using arrays in c?

Use a linked-list.


Can a linked list implemented using an array be accessed by giving the location?

A linked list implemented with an array defeats the purpose of using a linked list, which is to address the memory allocation problems associated with arrays.


What is the differnce between linked list and arrays?

A linked list is a series of elements, each containing a pointer or index to the next element in the list. You can dynamically add and delete elements in the list. An array is a contiguous block of repeated elements. Since each element is address-wise adjacent to the next element, there is no need for pointers or indexes to the "next" element. You can not dynamically add and delete elements in an array, although you can create "dynamic arrays" with (templates and) classes that auto-resize themselves. The STL does this for you, but it is a good exercise to implement it yourself.


How do you represented linked list in memory?

Memory Representation of Linear Linked List:Let LIST is linear linked list. It needs two linear arrays for memory representation. Let these linear arrays are INFO and LINK. INFO[K] contains the information part and LINK[K] contains the next pointer field of node K. A variable START is used to store the location of the beginning of the LIST and NULL is used as next pointer sentinel which indicates the end of LIST. It is shown below:


Can you simulate linked list using arrays?

I would say no, but it really depends on your point of view. An array and a linked list can both hold the same data; the only difference is how they do so. The definition of a linked list is a sequence of connected nodes. An array is a contiguous block of memory, so you can think of an array as a linked list in which each element is spacially connected to the next.


Disadvantages of linked lists in c?

Because the C programming language leaves the responsibility for memory allocation and pointers entirely with the programmer, the disadvantage of linked lists over some other linear data structures (such as arrays) is that the bear a risk or memory leaks and invalid pointers. The fact that the size of a linked list is generally not deterministic is also commonly viewed a disadvantage over statically linked linear containers (e.g. arrays) in some systems, primarily in embedded systems. Compared to containers of higher order (such as trees or hash tables), search operations in a linked list are generally slower. Compared to a double linked list, removal and insertion of items (except head and tail) is generally more expensive.


Comparison between linked and array lists?

The difference between linked and array lists is in how they are stored. Arrays are a predefined block of memory into which information is stored or recalled. Any data element from an array can be retrieved in O(1) time. But if you need to increase the size of the array, you would need to create a new one and copy the contents of the old one into it. Linked lists are usually dynamically allocated during run time, and the only way to get to a node (a data element) is to traverse the list until you find it which makes the retrieval time slower O(n) . The benefit of a linked list is that you can insert new data elements without having to copy or move all the elements in the list as you would in an array. The linked list is better for large databases that need to add and remove items often, whereas arrays are better for list sizes that generally don't change.


What is the difference between arrays and linked list?

An array is a list of objects.A linked list is made up of nodes each containing a value then the pointer to the next node.Linked list exampleNode 1:dog,continued in node 4Node 2:undefinedNode 3:mouse,end of listNode 4:cat,continued in node 3Array example:dog,cat,mouse,pony,horse


What are the 2 systems villi are linked between?

Villi are linked between the digestive systme and excretory system.


Difference between arrays and linked list?

each element in a linked list contains, in addition to data, one or more pointers to other element(s) in the list. such data structures are capable of changing size at runtime according to the needs of the programan array is simply a preallocated block of data elements. once allocated at compile/link time its size can never be changed


What are disadvantages of a linked list over an array?

A linked list is better bcoz: 1. A linked list can be grown to any size whereas a statically allocated array is of a fixed size and hence can cause problems if you try to insert beyond that. 2. Deletion of elements is a problem in arrays. You either have to move all the elements following the deleted element forward which is a waste of time or you have to place a sentinel indicating that the element in that position is deleted which causes a waste of space. On the flip side, arrays are easier to handle because their memory allocation is done once and for all(atleast till you need more space and have to reallocate) and because there is no hassle with pointers.


What rivers linked lands between the coast and the Appalachian Mountains with the sea?

what rivers linked lands between the coast and the appalachin mountains with the sea