STACK "Linked List Implementation" In C++
im going to show you the implementation of of "Stack" "First In Last Out" In Linked List Implementation in C++ Language
You will find the code splitted to three parts at the first part stack.h we are going to define function's header and at the second part stack.cpp we are going to implement this function and in the last part main.cpp we are going call this function
Implementation Of Stack Array-Based
Stack.h
typedef int INFO; typedef struct node { INFO data; struct node *next; }Node; typedef struct stacknode { Node *top; int size; }StackNode; void CreateStack(StackNode *); int StackEmpty(StackNode *); int StackFull(StackNode *); void Push(StackNode *,INFO *); void Pop(StackNode *,INFO *); int StackSize(StackNode *); void TraverseStack(StackNode *,void(*pvisit) (INFO)); void CopyStack(StackNode *,StackNode *);
Stack.Cpp
Main.Cpp
You Can Download It From Here : http://goo.gl/YttE4L
#include "STACKLINKED.h" #include <stdlib.h> void CreateStack(StackNode *ps) { ps->top=NULL; ps->size=0; } int StackEmpty(StackNode *ps) { return (ps->top==NULL); } int StackFull(StackNode *ps) { return 0; } void Push(StackNode *ps,INFO *item) { Node *p=(Node *)malloc(sizeof(Node)); p->data=*item; p->next=ps->top; ps->top=p; ps->size++; } void Pop(StackNode *ps,INFO *item) { Node *temp; *item=ps->top->data; temp=ps->top; ps->top=ps->top->next; free(temp); ps->size--; } int StackSize(StackNode *ps) { return ps->size; } void TraverseStack(StackNode *ps,void(*pvisit) (INFO)) { Node *q=ps->top; while(q) { (*pvisit)(q->data); q=q->next; } } void CopyStack(StackNode *ps,StackNode *ps1) { Node *q=ps->top; Node *pt=NULL; while(q) { Node *Nq=(Node *) malloc(sizeof(Node)); Nq->data=q->data; Nq->next=pt; pt=Nq; q=q->next; } Node *q2=pt; while(q2) { Node *Nq=(Node *) malloc(sizeof(Node)); Nq->data=q2->data; Nq->next=ps1->top; ps1->top=Nq; q2=q2->next; } }
Main.Cpp
#include "STACKLINKED.h" #include <stdio.h> #include <stddef.h> void Display(INFO e) { printf("%d\n",e); } int main() { int ch; INFO e; StackNode sn,sn1; CreateStack(&sn); CreateStack(&sn1); printf("\nChoose 1 To Push into stack\nChoose 2 To Pop from stack\nChoose 3 To Copy FROM Stack To Another\nChoose 4 To Copy Stack Number 2\nChoose 5 To Size Of Stack\nChoose 6 To TraversStack\n"); scanf("%d",&ch); while(ch!=7) { if(ch==1) { printf("Enter Data To Push it into stack\n"); scanf("%d",&e); if(!StackFull(&sn)) { Push(&sn,&e); } else printf("Stack Full .. Sorry !,Icant Push IT\n"); } else if(ch==2) { if(!StackEmpty(&sn)) { Pop(&sn,&e); printf("\nThe Item is : %d\n",e); } else printf("Stack Empty .. Sorry !\n"); } else if(ch==3) { /*Implementation Level*/ CopyStack(&sn,&sn1); printf("Copied Successfully\n"); } else if(ch==4) { if(!StackEmpty(&sn1)) { Pop(&sn1,&e); printf("\nThe Item is : %d\n",e); } else printf("Stack Empty .. Sorry !\n"); } else if(ch==5) { printf("Size Of Stack Is : %d\n",StackSize(&sn)); } else if(ch==6) { printf("You Are Going To Traverse This Stack\n"); TraverseStack(&sn,&Display); printf("Successfully Traversed\n"); } printf("\nChoose 1 To Push into stack\nChoose 2 To Pop from stack\nChoose 3 To Copy FROM Stack To Another\nChoose 4 To Copy Stack Number 2\nChoose 5 To Size Of Stack\nChoose 6 To TraversStack\n"); scanf("%d",&ch); } return 0; }
You Can Download It From Here : http://goo.gl/YttE4L

Leave a Comment