c语言,链表

时间:2023-12-18 12:25:02

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 = ;
L = (NODE*) malloc(sizeof(NODE)); L->next =;
for(i = ; 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 = ;
free(p);
p = ; return ;
} int delete_link_list(LIST *L)
{
NODE* pd = L->next; while(L->next != ){
pd = L->next;
L->next = L->next->next;
delete_node(pd);
}
delete_node(L); return ;
} int show_link_list(LIST *L)
{
NODE* ps = L->next;
int i = ; while(ps != ){
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 ;
}
int t2()
{
NODE *L = ;
int n = ; 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 ;
}
/*
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
请按任意键继续. . .
*/