DATA STRUCTURE basic

  1. // data structure --- link list5
  2. #include <stdio.h>
  3. #include <stdlib.h>
  4. #include <iostream>
  5. using namespace std;
  6. struct Node {
  7.     int data;
  8.     struct Node *next; 
  9. };
  10. typedef struct Node LNode;
  11. typedef LNode *List;
  12. List first = NULL;
  13. // create backward
  14. // first --> newnode1(data = 1, next = NULL )
  15. // first --> newnode2(data=2, next=newnode1) --> neewnode1(data = 1, next = NULL )
  16. void createList(int len, int *array){
  17.     int i;
  18.     List NewNode;
  19.     for ( i=0 ; i<len ; i++ )
  20.         {
  21.         NewNode = (List)malloc(sizeof(LNode)); 
  22.         NewNode -> data = array[i];
  23.         NewNode -> next = first;
  24.         first = NewNode;   
  25.         cout << " Node " << i << " data " << NewNode->data << " link " << NewNode->next <<endl;
  26.         }  
  27. }
  28. int main()
  29. {
  30. int data[6] = {0,1,2,3,4,5};
  31. cout <<" create linked list "<<endl;
  32. createList ( 6 , data );
  33. List current = first;  
  34. cout <<" Result "<<endl;
  35. while ( current != NULL ){
  36.     cout << " data " << current->data << " link " << current->next <<endl;
  37.     current = current->next;
  38. }  
  39.    
  40. }

留言

這個網誌中的熱門文章

Codeforces --- string task

Uva 674 ---- coin change

codeforces 271A --- beautiful year