Description on the STACKS keywords…..

Asked By 0 points N/A Posted on -
qa-featured

Hi! I have studied this thing that stacks have only one opening for inserting values in it and deleting them from the same opening. Stacks use the method of LIFO(last in first out). But what KEYWORDS are used for inserting the values and for deletion as well? also explain that stacks can be used only in ARRAY or they can be used in LINKEDLIST also? 

SHARE
Best Answer by JonathanW
Best Answer
Best Answer
Answered By 0 points N/A #85943

Description on the STACKS keywords…..

qa-featured

A stack is a list of elements in which an element may be inserted or deleted only at one end called top of the stack. There are two basic operations used in stacks. Insertion of elements into stack is called as Push and Removal of elements from the stack is called as Pop. Stack can be implemented in both Arrays and Linked lists. Stacks follow the LIFO structure that means we insert an element through an open end one after other. This way the element first inserted will be at the bottom and the element inserted at the end will be at the top. Similarly removing an element also depends on the position of the element in the stack. The element at the bottom of the stack requires   that all the elements to be removed first, while the element at the top needs just single operation.

In C++ there are no keywords defined for stack operations. User has to declare functions in order to Push and Pop elements to a stack. Below are sample implementations of pop and push in arrays and linked lists. Hope you understand the code.

Answered By 0 points N/A #85945

Description on the STACKS keywords…..

qa-featured

As you mentioned, stack is following the concept of first in last out which is known as (FILO). Process on stacks follows three fundamental operations. 1- Push operation which adds a new item to the top of the stack where the stack has a free space to insert in. 2- Pop which retrieve the most top item in the stack and deletes it. 3-The last one stack top which retrieve the most top item in the stack without deleting it. Restricted data structure, because only a small number of operations are performed on it. About the relationship between stack and linked list, sure stack works with linked list. The following sample code illustrate how stacks works with linked list:

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 noden", 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 underflown", stderr);
           abort();
        } else {                                     /* pop a node */
            STACK *top = *head;
            int value = top->data;
            *head = top->next;
            free(top);
            return value;
        }
    }

I hope that helps you.
Regards John.

Related Questions