c语言,链表
#include "stdafx.h" #include <stdio.h> #include <stdlib.h> struct Node{ struct Node* next; int data; }; typedef struct Node NODE; typedef struct Node LIST; LIST *creat_link_list(LIST *L, int n) { int i; NODE *p = 0; L = (NODE*) malloc(sizeof(NODE)); L->next =0; for(i = 0; i < n; i++) { p = (NODE*) malloc(sizeof(NODE)); printf("input a data to be inserted:"); scanf("%d", &p->data); p->next = L->next; L->next = p; } return L; } int delete_node(NODE* p) { // static int s_i =1; // printf("delete %d\n", s_i++); p->next = 0; free(p); p = 0; return 0; } int delete_link_list(LIST *L) { NODE* pd = L->next; while(L->next != 0){ pd = L->next; L->next = L->next->next; delete_node(pd); } delete_node(L); return 0; } int show_link_list(LIST *L) { NODE* ps = L->next; int i = 0; while(ps != 0){ printf("node%d: %d,", ++i , ps->data); ps = ps->next; }; // do{ //错误 // printf("node%d: %d,", ++i , ps->data); // ps = ps->next; // }while(ps->next != 0); return 0; } int t2() { NODE *L = 0; int n = 3; L = creat_link_list( L, n); printf("\ncreat_link_list over\n"); show_link_list(L); printf("\nshow_link_list over\n"); delete_link_list(L); printf("\ndelete_link_list over\n"); return 0; } /* input a data to be inserted:1 input a data to be inserted:2 input a data to be inserted:3 creat_link_list over node1: 3,node2: 2,node3: 1, show_link_list over delete_link_list over 请按任意键继续. . . */