DATA STRUCTURE basic
- #include <iostream>
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- int main()
- {
- typedef struct list_node *list_pointer;
- typedef struct list_node{
- int data;
- list_pointer link;
- }listNode;
- // declare variables
- list_pointer first,second,third,temp;
- first = (list_pointer)malloc(sizeof(listNode));
- second = (list_pointer)malloc(sizeof(listNode));
- third = (list_pointer)malloc(sizeof(listNode));
- temp = (list_pointer)malloc(sizeof(listNode));
- // data
- first -> data = 10;
- second -> data = 20;
- temp -> data = 30;
- third -> data = 40;
- // link first --> second --> third --> NULL
- first -> link = second;
- second -> link = third;
- third -> link = NULL;
- // insert link temp --> first --> second --> third --> NULL
- temp -> link = first;
- cout << "first link is: "<<endl;
- printf("%d",temp->link);
- printf("\n");
- printf("%d",first);
- printf("\n");
- cout << "second link is: " << endl;
- printf("%d",first->link);
- printf("\n");
- printf("%d",second);
- printf("\n");
- free(first);
- free(second);
- free (third);
- free(temp);
- }
留言
張貼留言