answersLogoWhite

0


Best Answer

You would not use an array for a stack or queue, you would use a singly-linked list. The problem with arrays is that they must grow and shrink as items are inserted or extracted, which requires the entire array to be copied, whereas linked-lists can grow and shrink dynamically without the need to copy any elements. The only reason to use an array to implement a list is when you require random access in constant time. Queues and stacks only require constant time access to the head or tail of the list, and a linked list can easily achieve that.

Implementing a stack as an array requires that new elements be inserted at the end of the array. If the array is full, it must be reallocated in order to make space for a new element. Extraction always occurs at the end of the array. While this releases elements for insertions, if they go unused for any length of time they are simply wasting memory. Thus the array must be reallocated occasionally to free the redundant memory, which is highly inefficient.

Implementing a queue has similar issues as all insertions are at the end of the array, however it is further complicated by the fact extractions must occur at the beginning of the array. Thus every element must be copied to its preceding element every time an element is removed from the head of the queue. This frees up the end of the array, but the need to reallocate and copy elements is again, highly inefficient.

Linked lists overcome all these problems such that no memory is ever in a redundant state, and no copying is ever required.

User Avatar

Wiki User

11y ago
This answer is:
User Avatar
More answers
User Avatar

Wiki User

11y ago

A stack is easily implemented with an array and a top-of-stack marker (TOS), implemented as an array index.

At initialization time, the TOS would be initialized to -1 or 0 depending on the implementation: initialize to -1 if the PUSH operation uses pre-increment, or 0 if the PUSH uses post-increment. The POP operation uses the reverse (post-decrement or pre-decrement, respectively).

Following is a crude example using pre-increment for PUSH, not including boundary checks:

int tos = -1;

DataType array[MAX_STACK_SIZE];

void push(DataType v) {

array[++tos] = v;

}

DataType pop() {

return array[tos--];

}

This answer is:
User Avatar

User Avatar

Wiki User

11y ago

A stack (first in, last out) is simpler. You just need an array and a separate value holding the number of items currently in the array.

Here's a very simple example; DON'T do it like this "for real," but it should be easy to understand:

#define STACK_SIZE 100 /* or 1000 or dynamically allocate, or whatever */

int number_of_items = 0;

int the_stack[STACK_SIZE];

void push_to_stack(int the_number)

{

if (number_of_items < STACK_SIZE)

{

the_stack[number_of_items] = the_number;

++number_of_items;

}

}

int pop_from_stack()

{

int return_value = 0;

if (number_of_items > 0)

{

--number_of_items;

return_value = the_stack[number_of_items];

}

return return_value;

}

Yes, indeed: I could have just said:

the_stack[number_of_items++] = the_number

and

return_value = the_stack[--number_of_items]

but this is clearer. Feel free to use the less clear way if you like, but my purpose here is just to be as clear as I can.

To do a queue (first in, first out), you need to keep track of which is the first item and which is the last one, because you keep "using up" the first one when you pop one off, and you add after the last one. And, eventually, you will either "run out" of your buffer because the active area keeps moving, or else you need a way to "circle back" to the beginning when you hit the end (some people might call this a "ring buffer"). This added complication is why many people think it's easier to use a linked list (instead of an array) for a queue.

I'm not going to write the code though, because that would be doing your homework for you. The guidelines in the above paragraph should point you to the right way.

This answer is:
User Avatar

User Avatar

Wiki User

9y ago

You will need to pick up one end and put it back into the stack to remove the last item. A queue you can hold the head and advance it through the array.

This answer is:
User Avatar

User Avatar

Wiki User

12y ago

bhenchod

This answer is:
User Avatar

Add your answer:

Earn +20 pts
Q: How do you represent stack and queue by using one dimensional array?
Write your answer...
Submit
Still have questions?
magnify glass
imp
Related questions

Data structure algorithms using C?

array,linklist,queue,stack,tree,graph etc...


What are the the two ways in representing the stack?

one-dimensional array, and doubly-linked list.


What are the different between stack and queue?

A stack is generally First In, Last Out, and a queue is First In First Out.Item can be added or removed only at one end in stack and in a queue insertion at the rear and deletion from the front.The basic operation of stack are 'push' and 'pop', on other hand of queue are 'enque' and 'dequeue'.


What is the meaning of a stack overflow?

A stack overflow is a programming term used to identify when a function tries to access memory from a stack that does not exist. A stack, such as a queue or array, contains a limited number of memory spaces set aside when it is created. For example, if an array has 8 objects in it and a function tried to access an item at slot nine, which doesn't exist, it would cause a stack overflow.


What is the difference between stack and array?

Briefly, there are two main differences between an array and a stack. Firstly, an array can be multi-dimensional, while a stack is strictly one-dimensional. Secondly, an array allows direct access to any of its elements, whereas with a stack, only the 'top' element is directly accessible; to access other elements of a stack, you must go through them in order, until you get to the one you want. I hope this answered your question. OK?


How many types of arrays in c?

An array is simply a contiguous block of memory containing two or more elements. There are two types of array: a static array which is allocated on the stack at compile time; and a dynamic array which is allocated on the heap at runtime. Both can be one-dimensional or multi-dimensional. A one-dimensional array can be likened to a row (or column) of chessboard squares, with as many squares as required to store all the elements. A multi-dimensional array is any array with two or more dimensions. A two-dimensional array can be likened to the whole chessboard, where any square can be identified by its row and column index. However the dimensions needn't be equal. A two-dimensional array can also be imagined as a one-dimensional array where every element is simply another one-dimensional array. Three-dimensional arrays can be likened to a cube, or as a one-dimensional array of two-dimensional arrays. A four-dimensional array can be linked to a one-dimensional array of three-dimensional arrays, and so on. Although every one-dimensional array must be allocated in contiguous memory, multi-dimensional arrays can be dynamically allocated so that each dimension is itself a separately allocated one-dimensional array of pointers to the next dimension, making it possible to allocate extremely large arrays over a series of smaller allocations rather than as a single contiguous block.


Why does LIFO order follows in stack and why does FIFO order follows in queue?

LIFO and stack are synonyms, so are FIFO and queue.


What is difference between stack snd queue?

In stack , the object which is last in will be first out (LIFO), whereas in queue the object which is first in will be first out (FIFO).


Why are the array and linked list used as stack queue and others?

Arrays are beneficial whenever you need constant-time, random-access to any element within a set of data and the number of elements doesn't change too much (enlarging an array may require the entire array be copied to a larger block of free memory, which is expensive in terms of performance). Linked lists are beneficial when the number of elements is highly variable and you also need constant time access to the head (as per a stack) or both the head and tail (as per a queue). Neither a queue nor a stack requires any traversal whatsoever, so the lack of random access is immaterial. If a set of data is variable and must remain in sorted order (such as a priority queue), then a heap or self-balancing binary tree is the most efficient structure to use.


Is cursor implementation possible in queue or stack?

yes,cursor implementation possible in priority queue.


Write a program in c language to reverse elements of a queue?

There are many ways to reverse the order of the elements in a queue. Provided that you have access to the implementation of the queue, it is of course easy to read the elements from the tail end rather than the front end, thus reversing the elements. However, considering the queue as a black box, and assuming the queue only allows for its characteristic operations (removal of head element, addition to tail), the best method to reverse the elements in a queue to engage a stack. You'd remove the elements from the queue (always reading the head of the queue), and push each element onto the stack. When the queue is empty, you reverse that process: pop each element from the stack until it is empty, and add each element in this order to the end of the queue. Your queue will have the exact same elements as in the beginning, but in reverse order. The exact implementation of this in C, or in any other programming language, is trivial, but the exact source code depends on the implementation of queue and stack containers. Following is pseudocode: Queue&lt;Item&gt; reverse (Queue&lt;Item&gt; queue) { Stack&lt;Item&gt; stack; Item item; while (queue.remove(&amp;item)) { stack.push(item); } while(stack.pop(&amp;item)) { queue.add(item); } return queue; }


Double ended stack?

Double ended queue