If we keep some books on our table one on top of another, we call it a stack of books. This informal idea can be extended to the official understanding of stack data structure.
In this context, we do understand that the books are accessible from the top of the stack of books, not from any intermediate positions. If you want to put another book that is to be kept on top of the books and if we want to remove a book, that is to be removed from the top. The same concept is used in this stack data structure.
Basically, a stack is a collection of data elements that are kept in an order, where elements are added and removed on the same side of the elements. These operations are known as push (insertion) and pop (deletion).
A very common example of the use of a stack is a recursive function. In a recursive function, the state of the caller function is stored in the stack. And after completion of the called function, the control of the program moves to the caller.
Hence, in which order the elements are inserted, in the reverse order the elements are deleted. Thus, we can consider that one end of the stack is blocked and operations are performed at the other end. In the following figure dark end represents that the operations can not be performed at that end.
Applications
Some of the common applications of the stack are
- Function call / Recursive function call
- Infix to postfix / prefix conversion
- Postfix expression evaluation etc.
These applications will be discussed in the following tutorials.
Definition of stack
A Stack is a collection of data elements, where elements are inserted and deleted at the same end. These operations are known as push (insertion) and pop (deletion).
Hence, we can understand in which order the elements are inserted, in the reverse order the elements are deleted. The operations on a stack follow a well-known technique, called Last In First Out (LIFO).
Operations on Stack
Generally, two operations are performed in the Stack data structure. The operations are
- insertion of new elements to the stack, known as Push operation and
- removal of existing elements from the stack, known as Pop operation.
There is another operation, which is used to read the topmost element of the stack without performing any modification to the stack. The operation is known as Peek.
Stack using Array
How to define and create Stack?
#define CAPACITY 10 typedef struct Stack { int data[CAPACITY]; int topindex; } stack;
void createStack(stack *s) { s->topindex = -1; }
Push Operation
void push(stack *s, int data) { if(isFull(s)) { printf("Stack is Full."); } else { s->topindex++; s->data[s->topindex] = data; } }
Pop Operation
int pop(stack *s) { int temp = -9999; if(isEmpty(s)) { printf("Stack is Empty."); } else { temp = s->data[s->topindex]; s->topindex--; } return temp; }
Checking whether the Stack is full or not?
int isFull(stack *s) { if(s->topindex == CAPACITY - 1) return 1; else return 0; }
Checking whether the Stack is empty or not?
int isEmpty(stack *s) { if(s->topindex == -1) return 1; else return 0; }
Complete code
Stack using Linked List
A Stack is a collection of data elements, where elements are inserted and deleted at the same end. These operations are known as push (insertion) and pop (deletion).
A very common example of use of stack is recursive function. In a recursive function, state of the caller function is stored in stack. And after completion of the called function the control of the program moves to the caller.
Hence, in which order the elements are inserted, in the reverse order the elements are deleted.
How to define Stack
typedef struct Node { int data; struct Node *next; } node;
Push operation
void push(node **head, int item) { node *newnode = (node *)malloc(sizeof(node)); newnode->data = item; if(*head == NULL) newnode->next=NULL; else newnode->next = *head; *head = newnode; }
Pop operation
int pop(node **head) { node *temp; int data = -1; if(*head != NULL) { temp = *head; *head = (*head)->next; data = temp->data; free(temp); } return data; }
Complete code
No comments:
Post a Comment
Note: Only a member of this blog may post a comment.