DATA STRUCTURE basic
- // data structure --- link list5
- #include <stdio.h>
- #include <stdlib.h>
- #include <iostream>
- using namespace std;
- struct Node {
- int data;
- struct Node *next;
- };
- typedef struct Node LNode;
- typedef LNode *List;
- List first = NULL;
- // create backward
- // first --> newnode1(data = 1, next = NULL )
- // first --> newnode2(data=2, next=newnode1) --> neewnode1(data = 1, next = NULL )
- void createList(int len, int *array){
- int i;
- List NewNode;
- for ( i=0 ; i<len ; i++ )
- {
- NewNode = (List)malloc(sizeof(LNode));
- NewNode -> data = array[i];
- NewNode -> next = first;
- first = NewNode;
- cout << " Node " << i << " data " << NewNode->data << " link " << NewNode->next <<endl;
- }
- }
- int main()
- {
- int data[6] = {0,1,2,3,4,5};
- cout <<" create linked list "<<endl;
- createList ( 6 , data );
- List current = first;
- cout <<" Result "<<endl;
- while ( current != NULL ){
- cout << " data " << current->data << " link " << current->next <<endl;
- current = current->next;
- }
- }
留言
張貼留言