Operations on lists

Posted by marian on June 29, 2009

  1. #include <stdio.h>
  2. #include <stdlib.h>
  3.  
  4. typedef struct node
  5. {
  6.         int key;
  7.         struct node *next;
  8. }list;
  9.  
  10. void print(list *root)
  11. {
  12.      list *i;
  13.      for(i=root; i!=NULL; i=i->next)
  14.               printf(" %d",i->key);
  15.      printf("\n");
  16. }
  17. //create a singly linked list
  18. list *addFirst(list *root, int val)
  19. {
  20.      list *p;
  21.      p=(list*) malloc(sizeof(list));
  22.      p->key=val;
  23.      p->next=root;
  24.      root=p;
  25.      return root;    
  26. }
  27. list *addLast(list *root, int val)
  28. {
  29.      list *p;
  30.      p=(list*)malloc(sizeof(list));
  31.      p->next=NULL;
  32.      p->key=val;
  33.      if(root==NULL) root=p;
  34.      else {
  35.           list *i;
  36.           for(i=root; i->next!=NULL;i=i->next);
  37.           i->next=p;
  38.           }
  39.      return root;
  40. }
  41. list *create(list *root)
  42. {
  43.      root=NULL;
  44.      int val;
  45.      while(1==scanf("%d",&val))
  46.      {
  47.           if(val>0) root=addFirst(root,val);
  48.           else root=addLast(root,val);
  49.           print(root);
  50.      }
  51.      addLast(root,-1);
  52.      return root;                
  53. }
  54.  
  55. list *findKey(list *root, int key)
  56. {
  57.      list *i;
  58.      for(i=root; i!=NULL; i=i->next)
  59.          if(i->key==key) return i;
  60.      return NULL;
  61. }
  62. int deleteList(list* root)
  63. {
  64.     list *p;
  65.     while(root!=NULL)
  66.     {
  67.        p=root;
  68.        root=root->next;
  69.        free(p);
  70.     }
  71. }
  72.  
  73. int main()
  74. {
  75.     list *root;
  76.     root=create(root);
  77.     print(root);
  78.     if(findKey(root,0)!=NULL) printf("key 0 has been found");
  79.     deleteList(root);
  80.     system("pause");
  81.     return 0;
  82. }

undefined reference to `__gxx_personality_v0′

Posted by marian on June 29, 2009

This happens when you try to compile and link a *.cpp program with gcc.

  1. gcc -o hello.o hello.cpp

The solution is to change the extention (if you can) or compile it with g++

  1. gcc -o hello.o hello.c