|
5鱼币
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int data;
- struct node* next;
- } Node;
- void insert(Node* head, int data){
- // create new node
- Node* newNode = (Node*)malloc(sizeof(Node));
- newNode->data = data;
- newNode->next = NULL;
- if(head->next == NULL){
- head->next = newNode;
- }else{
- Node* temp = head->next;
- while(temp){
- printf("%x \n", temp);
- temp = temp->next;
- }
- temp = newNode;
- }
- }
- int main(){
- Node head;
- head.data = -1;
- head.next = NULL;
- insert(&head, 10);
- insert(&head, 11);
- insert(&head, 12);
- Node* temp = head.next;
- while(temp != NULL){
- printf("%d \n", temp->data);
- temp = temp->next;
- }
- }
复制代码
- #include <stdio.h>
- #include <stdlib.h>
- typedef struct node {
- int data;
- struct node* next;
- } Node;
- void insert(Node* head, int data){
- // create new node
- Node* newNode = (Node*)malloc(sizeof(Node));
- newNode->data = data;
- newNode->next = NULL;
- if(head->next == NULL){
- head->next = newNode;
- }else{
- Node* temp = head->next;
- //修改如下 输出结果为12 11 10
- newNode->next=temp;
- head->next=newNode;
-
-
- while(temp){
- printf("%x \n", temp);
- temp = temp->next;
- }
- }
- // temp = newNode;
- }
- int main(){
- Node head;
- head.data = -1;
- head.next = NULL;
- insert(&head, 10);
- insert(&head, 11);
- insert(&head, 12);
- Node* temp = head.next;
- while(temp != NULL){
- printf("%d \n", temp->data);
- temp = temp->next;
- }
- }
复制代码
|
|