使用结构指针实现链表
2014-11-21 14:34
288 查看
模拟的是一个使用结构链表(而不是数组)来存放电影信息
#include<stdio.h>
#include<stdlib.h>// 提供malloc、free等函数原型
#include<string.h>// 提供strcpy等函数原型
#define TSIZE 45// 存放电影名的数组大小
struct film{
char title[TSIZE];// 电影的名字
intrating;// 对电影的评分
struct film *next;// 指向链表的下一个结构
};
int main(void){
struct film *head = NULL;
struct film *prev,*current;
char input[TSIZE];
// 收集并存储信息
puts("Enter first movie title:");
while(gets(input) != NULL && input[0] !='\0'){
current = (struct film*)malloc(sizeof(struct film));
if(head == NULL){// 第一个结构体指针
head = current;
}else{// 后续结构
prev->next = current;
}
current->next = NULL;
strcpy(current->title,input);
puts("Enter your rating<0~10>");
scanf("%d",¤t->rating);
while(getchar() !='\n'){
continue;
}
puts("Enter next movie title(empty line to stop):");
prev = current;
}
// 输出存储的电影列表
if(head == NULL){
printf("NO data entered");
}else{
printf("Here is the movie list:\n");
}
current = head;
while(current != NULL){
printf("Movie:%s Rating:%d\n",current->title,current->rating);
current = current->next;
}
// 任务已经完成,因此释放所分配的内存
current = head;
while(current != NULL){
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
运行程序,输入看过的电影的电影名和评分,在提示
Enter next movie title(empty line to stop):
后输入回车以跳出最外层的while循环
程序负责打印用户输入的信息,并释放相应的内存
#include<stdio.h>
#include<stdlib.h>// 提供malloc、free等函数原型
#include<string.h>// 提供strcpy等函数原型
#define TSIZE 45// 存放电影名的数组大小
struct film{
char title[TSIZE];// 电影的名字
intrating;// 对电影的评分
struct film *next;// 指向链表的下一个结构
};
int main(void){
struct film *head = NULL;
struct film *prev,*current;
char input[TSIZE];
// 收集并存储信息
puts("Enter first movie title:");
while(gets(input) != NULL && input[0] !='\0'){
current = (struct film*)malloc(sizeof(struct film));
if(head == NULL){// 第一个结构体指针
head = current;
}else{// 后续结构
prev->next = current;
}
current->next = NULL;
strcpy(current->title,input);
puts("Enter your rating<0~10>");
scanf("%d",¤t->rating);
while(getchar() !='\n'){
continue;
}
puts("Enter next movie title(empty line to stop):");
prev = current;
}
// 输出存储的电影列表
if(head == NULL){
printf("NO data entered");
}else{
printf("Here is the movie list:\n");
}
current = head;
while(current != NULL){
printf("Movie:%s Rating:%d\n",current->title,current->rating);
current = current->next;
}
// 任务已经完成,因此释放所分配的内存
current = head;
while(current != NULL){
free(current);
current = current->next;
}
printf("Bye!\n");
return 0;
}
运行程序,输入看过的电影的电影名和评分,在提示
Enter next movie title(empty line to stop):
后输入回车以跳出最外层的while循环
程序负责打印用户输入的信息,并释放相应的内存
相关文章推荐
- 链表操作(从尾到头打印链表,使用栈结构实现)
- 结构体内可以包含自身的指针(链表实现方法)或 引用 但不可以包含自身变量即递归 (因为这样一来结构体大小就无法确定了)
- C++使用单指针Struct实现双向链表
- 树用链表实现(链表使用二重指针减少判断以及空间复杂度)
- 使用链表实现环结构以解决约瑟夫环问题
- 数据结构实现之无序符号表SequentialSearchST(使用链表实现)
- 使用结构和指针(单/双链表)
- 使用链表来实现栈结构(希望大神评论一下是否正确)
- 使用LINUX C实现一个链表,要求:链表节点构成:姓名、分数、下一个节点指针...
- 数据结构与算法-----堆栈-使用链表(链式结构)实现
- 递归实现有序链表(不需要使用两个指针)
- 数据结构之通用树(使用链表实现树的存储结构,双亲孩子表示法)
- 链表的Java实现(使用内部类作为指针的引用)
- C 数据结构使用数组和链表实现栈
- 数据结构——使用双端链表实现队列(java实现)
- 数据结构与算法-----队列-使用链表(链式结构)实现
- C++ 单链表基本操作分析与实现 链表 链表是一种物理存储单元上非连续、非顺序的存储结构,数据元素的逻辑顺序是通过链表中的指针链接次序实现的。链表由一系列结点(链表中每一个元素称为结点)组成,结
- 使用单指针域实现双向链表
- C语言实现使用带头结点的单链表来构造栈结构
- 数据结构之链表的使用与实现