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     first --> temp --> second --> third --> NULL      
  29. temp -> link = first->link;
  30. first -> link = temp;
  31. cout << "first link is: "<<endl;
  32. printf("%d",first->link);
  33. printf("\n");
  34. printf("%d",temp); 
  35. printf("\n");
  36. cout << "second link is: " << endl;
  37. printf("%d",temp->link);
  38. printf("\n");
  39. printf("%d",second);
  40. printf("\n");
  41. free(first);
  42. free(second);
  43. free (third);
  44. free(temp);
  45. }


留言

這個網誌中的熱門文章

Codeforces --- string task

Uva 674 ---- coin change

codeforces 271A --- beautiful year