CreateNode and Display
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- typedef struct Node *list_pointer;
- struct Node {
- int data;
- list_pointer next;
- }LNode;
- typedef struct Node *List;
- List first;
- void createNode(int len, int *array)
- {
- List newnode;
- for ( int i=0 ; i<len ; i++ )
- {
- newnode = (List)malloc(sizeof(LNode));
- newnode->data = array[i];
- newnode->next = first;
- first = newnode;
- }
- }
- list_pointer invert(List lead)
- {
- list_pointer middle,trail;
- middle = NULL;
- while ( lead )
- {
- trail = middle;
- middle = lead;
- lead = lead->next;
- middle->next = trail;
- }
- return middle;
- }
- int main()
- {
- int array[4] ={ 0, 1, 2, 3};
- createNode(4,array);
- List current;
- current = first;
- while ( current!=NULL )
- {
- printf("[%d]",current->data);
- current = current->next;
- }
- printf("\n");
- }
ORIGIN
AFTER
- #include <stdio.h>
- #include <stdlib.h>
- using namespace std;
- typedef struct Node *list_pointer;
- struct Node {
- int data;
- list_pointer next;
- }LNode;
- typedef struct Node *List;
- List first;
- void createNode(int len, int *array)
- {
- List newnode;
- for ( int i=0 ; i<len ; i++ )
- {
- newnode = (List)malloc(sizeof(LNode));
- newnode->data = array[i];
- newnode->next = first;
- first = newnode;
- }
- }
- list_pointer invert(List lead)
- {
- list_pointer middle,trail;
- middle = NULL;
- while ( lead )
- {
- trail = middle;
- middle = lead;
- lead = lead->next;
- middle->next = trail;
- }
- return middle;
- }
- int main()
- {
- int array[4] ={ 0, 1, 2, 3};
- createNode(4,array);
- List current;
- current = invert(first);
- while ( current!=NULL )
- {
- printf("[%d]",current->data);
- current = current->next;
- }
- printf("\n");
- }
留言
張貼留言