DATA STRUCTURE basic

  1. / data structure ---- linked list-3       C/C++ language
  2. #include <iostream>
  3. #include <stdio.h>
  4. #include <stdlib.h>
  5. using namespace std;
  6. int main()
  7. {
  8. typedef struct list_node *list_pointer;
  9. typedef struct list_node{
  10. int data;
  11. list_pointer link;     
  12. }listNode; 
  13. // declare variables
  14. list_pointer first,second,third,temp;
  15. first = (list_pointer)malloc(sizeof(listNode));
  16. second = (list_pointer)malloc(sizeof(listNode));
  17. third = (list_pointer)malloc(sizeof(listNode));
  18. temp = (list_pointer)malloc(sizeof(listNode));
  19. // data
  20. first -> data = 10;
  21. second -> data = 20;
  22. temp -> data = 30;
  23. third -> data = 40;
  24. // link            first --> second --> third --> NULL
  25. first -> link = second;
  26. second -> link = third;
  27. third -> link = NULL;
  28. // insert link     temp --> first --> second --> third --> NULL      
  29. temp -> link = first;
  30. cout << "first link is: "<<endl;
  31. printf("%d",temp->link);
  32. printf("\n");
  33. printf("%d",first);
  34. printf("\n");
  35. cout << "second link is: " << endl;
  36. printf("%d",first->link);
  37. printf("\n");
  38. printf("%d",second);
  39. printf("\n");
  40. free(first);
  41. free(second);
  42. free (third);
  43. free(temp);
  44. }

留言

這個網誌中的熱門文章

Codeforces --- string task

Uva 674 ---- coin change

codeforces 271A --- beautiful year