Posts

Showing posts with the label stack

Implementation of Stack with Singly Linked List : C Program

Image
  Here is the code for implementation of stack with singly linked list : program code : #include<stdio.h> #include <stdlib.h> struct node {     int data;     struct node *next; }; struct node *top=NULL; void push(int d) {     struct node *newnode=(struct node*)malloc(sizeof(struct node));     newnode->data=d;     newnode->next=NULL;     if(top==NULL)     {         top=newnode;     }     else{         newnode->next=top;         top=newnode;     }     printf("\ndata %d has been pushed (entered to stack).",d); } void pop() {     struct node *p,*temp=top;     if(temp=NULL)     {         printf("\nstack is empty.");         return;     }     p=top;     top=top->next;     free(p);   ...

stack using array : C program

Image
  Here is the implementation of Stack using Array in C : Program code: #include <stdio.h> #define size 10 int stack[size]; int top=-1; void push(int x) {     if(top>=size)     {         printf("\noverflow");     }     else     {         top++;         stack[top]=x;     } } void pop() {     if(top<=0)     {         printf("\nstack is empty.");         return;     }     return stack[top--]; } void display() {     int i;     if(top<0)     {         printf("\nstack is empty");         return;     }     printf("\nstack elements are : ");     for(i=top;i>=0;i--)     {         printf("%d ",stack[i]);     }     printf("\n"); } i...