Download Implementations of stack menu driven program. Implementations of

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts
Transcript
Practical No.4
Date:-
Title:- Implementations of stack menu driven program.
Implementations of Infix to Postfix transformation and its evaluation program
What is stack?
a stack is a particular kind of abstract data type or collection in which the principal (or only)
operations on the collection are the addition of an entity to the collection, known as push
and removal of an entity, known as pop.[1] The relation between the push and pop
operations is such that the stack is a Last-In-First-Out (LIFO) data structure. In a LIFO data
structure, the last element added to the structure must be the first one to be removed. This
is equivalent to the requirement that, considered as a linear data structure, or more
abstractly a sequential collection, the push and pop operations occur only at one end of the
structure, referred to as the top of the stack. Often a peek or top operation is also
implemented, returning the value of the top element without removing it.
A stack may be implemented to have a bounded capacity. If the stack is full and does not
contain enough space to accept an entity to be pushed, the stack is then considered to be in
an overflow state. The pop operation removes an item from the top of the stack. A pop
either reveals previously concealed items or results in an empty stack, but, if the stack is
empty, it goes into underflow state, which means no items are present in stack to be
removed.
A stack is a restricted data structure, because only a small number of operations are
performed on it. The nature of the pop and push operations also means that stack elements
have a natural order. Elements are removed from the stack in the reverse order to the order
of their addition. Therefore, the lower elements are those that have been on the stack the
longest
Operations on Stack
In many implementations, a stack has more operations than "push" and "pop". An example
is "top of stack", or "peek", which observes the top-most element without removing it from
the stack. Since this can be done with a "pop" and a "push" with the same data, it is not
essential. An underflow condition can occur in the "stack top" operation if the stack is
empty, the same as "pop". Also, implementations often have a function which just returns
whether the stack is empty.
There are three basic operations on a stack: push onto a stack, pop off an element from a
stack, and verify whether the stack is empty or not. Here we will give pseudocode for
stack operations.
a) push operation
Algorithm push(newElement)
//increment index to the top element;
topElementIndex _ topElementIndex + 1
stack[topElementIndex] _ newElement
b) pop operation
Algorithm pop()
elementToReturn = stack[topElementIndex]
topElementIndex _ topElementIndex - 1
return elementToReturn
c) isEmpty operation
Algorithm isEmpty()
if topElementIndex = -1
return true
else
return false
Implementation
Array
The array implementation aims to create an array where the first element (usually at the
zero-offset) is the bottom. That is, array[0] is the first element pushed onto the stack and
the last element popped off. The program must keep track of the size, or the length of the
stack. The stack itself can therefore be effectively implemented as a two-element structure
in C:
typedef struct {
size_t size;
int items[STACKSIZE];
} STACK;
void push(STACK *ps, int x)
{
if (ps->size == STACKSIZE) {
fputs("Error: stack overflow\n", stderr);
abort();
} else
ps->items[ps->size++] = x;
}
int pop(STACK *ps)
{
if (ps->size == 0){
fputs("Error: stack underflow\n", stderr);
abort();
} else
return ps->items[--ps->size];
}
Linked list Implementation
The linked-list implementation is equally simple and straightforward. In fact, a simple singly
linked list is sufficient to implement a stackā€”it only requires that the head node or element
can be removed, or popped, and a node can only be inserted by becoming the new head
node.
typedef struct stack {
int data;
struct stack *next;
} STACK;
void push(STACK **head, int value)
{
STACK *node = malloc(sizeof(STACK)); /* create a new node */
if (node == NULL){
fputs("Error: no space available for node\n", stderr);
abort();
} else {
/* initialize node */
node->data = value;
node->next = empty(*head) ? NULL : *head; /* insert new head if any */
*head = node;
}
}
int pop(STACK **head)
{
if (empty(*head)) {
/* stack is empty */
fputs("Error: stack underflow\n", stderr);
abort();
} else {
STACK *top = *head;
int value = top->data;
*head = top->next;
free(top);
return value;
}
}
Output:1.Print outs of source code.
2.Printouts of output.
//pop a node