/*
* =====================================================================================
*
* Filename: list.c
*
* Description:
*
* Version: 1.0
* Created: 2012年04月21日 15时51分45秒
* Revision: none
* Compiler: gcc
*
* Author: Paathurnax Citizen, citizen2047@gmail.com
* Company: free work.student
*
* =====================================================================================
*/
#include <stdio.h>
#include <stdlib.h>
struct list{
int a;
struct list *next;
};
int main(){
struct list *cu;
struct list *prev;
struct list *head = NULL;
char *string = "";
int i;
//每次进行输入的时候,才对链表分配内存
while(scanf("%d",&i) == 1){
cu = (struct list *)malloc(sizeof (struct list));
if(cu == NULL){
printf("Memory not enough!\n");
}
if(head == NULL){
head = cu;
}else{
prev->next = cu;
}
cu->next = NULL;
cu->a = i;
prev = cu;
}
//对输出链表内容
cu = head;
while(cu != NULL){
printf("%d\n",cu->a);
cu = cu->next;
}
//清理链表
cu = head;
while(cu != NULL){
free(cu);
cu = cu->next;
}
return 0;
}
无聊写了一个小链表
最新推荐文章于 2024-11-01 20:05:40 发布