Created
April 18, 2013 18:33
-
-
Save jacobabrahamb4/5415113 to your computer and use it in GitHub Desktop.
stack using linked lists, will be edited later.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
//http://www.zentut.com/c-tutorial/c-stack-using-pointers/ | |
#include <stdio.h> | |
#include <stdlib.h> | |
typedef struct node{ | |
int data; | |
struct node* next; | |
} node; | |
node* create_node(int data) | |
{ | |
node* new_node=(node*)malloc(sizeof(node)); | |
if(new_node==NULL) | |
{ | |
printf("Error creating new node! \n"); | |
exit(1); | |
} | |
return new_node; | |
} | |
node* push(int data, node* top) | |
{ | |
node* new_node = create_node(data); | |
new_node->next = NULL; | |
if(top==NULL) return new_node; | |
else | |
{ | |
top->next = new_node; | |
return (top=new_node); | |
} | |
} | |
// use a node_capture to capture the released data and deallocate the memory allocated. | |
node* pop(node* head, node* top, node* node_capture) | |
{ | |
if(head==NULL || top== NULL ) return NULL; | |
node* cursor = head; | |
while(cursor->next!=top) cursor = cursor->next; | |
cursor->next = NULL; | |
node_capture = top; | |
return (top = cursor); | |
} | |
int main() | |
{ | |
// implemet methods to use those functions here | |
return 0; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment