Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Apr 25th, 2012  |  syntax: C  |  size: 1.60 KB  |  hits: 12  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct graphElement{
  5.        
  6.         int label;
  7.         struct graphElement *node;
  8.        
  9. } graphElement;
  10.  
  11. typedef struct graphLine{
  12.        
  13.         struct graphElement *nodes[2];
  14.         struct line *nextElement;
  15.        
  16. } graphLine;
  17.  
  18.  
  19. //
  20.  
  21. typedef struct stack{
  22.         graphLine *stackElement;
  23.         struct stack *nextItem;
  24. } stack;
  25.  
  26. stack *stackTop;
  27. int elementsOfStack = 0;
  28.  
  29. void createStack(){
  30.        
  31.         stack *newStack;
  32.         newStack = (stack*)malloc(sizeof(stack));
  33.         newStack -> stackElement = NULL;
  34.         newStack ->  nextItem = NULL;
  35.         stackTop = newStack;
  36.        
  37. }
  38.  
  39. graphLine *popStack(stack *stackItem){
  40.        
  41.         graphLine *tempLine;
  42.        
  43.         tempLine = stackItem -> stackElement;
  44.         stackTop = stackTop -> nextItem;
  45.         free(stackItem);
  46.        
  47.         return tempLine;
  48.        
  49. }
  50.  
  51. void pushStack(stack *stackItem, graphLine *newLine){
  52.        
  53.         if(stackTop->stackElement == NULL){
  54.                
  55.                 stackItem -> stackElement = newLine;
  56.                 stackTop = stackItem;
  57.                
  58.         } else{
  59.                
  60.                 stack *stackNewItem = (stack*)malloc(sizeof(stack));
  61.                 stackNewItem -> stackElement = newLine;
  62.                 stackNewItem -> nextItem = stackItem;
  63.                
  64.                 stackTop = stackNewItem;
  65.                
  66.         }
  67. }
  68.  
  69.  
  70. //
  71.  
  72. void createGraph(int *newLabels){
  73.        
  74.         int index = 0;
  75.        
  76.         if(!elementsOfStack){
  77.                 createStack();
  78.         }
  79.        
  80.         graphLine *newLine = (graphLine*)malloc(sizeof(graphLine));
  81.        
  82.         while(index < 2){
  83.                
  84.                 graphElement *newElement = (graphElement*)malloc(sizeof(graphElement));
  85.                 newElement -> label = newLabels[index];
  86.                 newLine -> nodes[index] = newElement;
  87.                
  88.                 pushStack(stackTop, newLine);
  89.                
  90.                 index++;
  91.                 elementsOfStack++;
  92.                
  93.         }
  94.        
  95. }
  96.  
  97. int main(){
  98.        
  99.        
  100.        
  101.     return 0;
  102. }