Skip to content

Instantly share code, notes, and snippets.

@jacobabrahamb4
Created April 18, 2013 18:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jacobabrahamb4/5415113 to your computer and use it in GitHub Desktop.
Save jacobabrahamb4/5415113 to your computer and use it in GitHub Desktop.
stack using linked lists, will be edited later.
//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