stack in data structure code example

Example 1: stack data structure python

#using doubly linked list
from collections import deque
myStack = deque()

myStack.append('a')
myStack.append('b')
myStack.append('c')

myStack
deque(['a', 'b', 'c'])

myStack.pop()
'c'
myStack.pop()
'b'
myStack.pop()
'a'

myStack.pop()

#Traceback (most recent call last):
 #File "<console>", line 1, in <module>
##IndexError: pop from an empty deque

Example 2: stack implementation

typedef struct Nodo{
   Elem val;
   struct Nodo *next;
} *Stack;
Stack Empty(){return NULL;}
bool IsEmpty(Stack a){return a==NULL;}
Elem Top(Stack a){return a->val;} 
Stack Pop(Stack l){return l->next;}
Stack Push(Elem x,Stack res){
    Stack nuevo=(Stack)malloc(sizeof(struct Nodo));
    nuevo->val=x;
    nuevo->next=res;
    return nuevo;
}

Tags:

Java Example