单双链表代码
来源:互联网 发布:visual studio写java 编辑:程序博客网 时间:2024/06/07 11:37
#include<stdlib.h>
typedef struct student_info
{
char stu_name[20];
int stu_score;
int index;
struct student_info *prei;
struct student_info *next;
} stu_info;
static stu_info *stu_info_list_create(void)
{
int n = 0;
stu_info *head,*tail,*new;
new = (stu_info*)malloc(sizeof(stu_info));
if(new == NULL) return;
printf("\ninut the name:");
scanf("%s",new->stu_name);
printf("\ninut the score:");
scanf("%d",&new->stu_score);
new->index = 1;
new->next = NULL;
new->prei = NULL;
//while(new->stu_score >= 0)
//while((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
while(1)
{
n = n+1;
if(n == 1)
{
head = new;
tail = head;
}
new = (stu_info*)malloc(sizeof(stu_info));
if(new == NULL) return;
printf("\ninut the name:");
scanf("%20s",new->stu_name);
if((strcmp(new->stu_name,"q")==0)||(strcmp(new->stu_name,"Q")==0))
break;
printf("\ninut the score:");
scanf("%d",&new->stu_score);
new->index = n;
new->prei = NULL;
new->next = NULL;
tail->next = new;
new->prei = tail;
tail = new;
}
return head;
}
static void stu_info_list_show_index(stu_info *head,int index)
{
stu_info *p = head;
while(p->index != index)
{
p = p->next;
}
printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
p = p->prei;
printf("\n%d,%s,%d",p->index,p->stu_name,p->stu_score);
}
static void stu_info_list_show(stu_info *head)
{
stu_info *p = head;
while(p != NULL)
{
printf("\n%d name:%-10s score:%d",p->index,p->stu_name,p->stu_score);
p = p->next;
}
printf("\n");
return;
}
static void destroy_list(stu_info *head)
{
stu_info *p = head;
stu_info *q;
while(p != NULL)
{
q = p->next;
free(p);
p->next = NULL;
p = q;
}
free(head);
head = NULL;
return;
}
static void write_stu_info_to_file(stu_info* head)
{
FILE *fp;
stu_info *p = head;
fp = fopen("stu_info.txt","w+");
fputs("the info of student:\n",fp);
while(p != NULL)
{
fprintf(fp,"%5d,%-10s,%3d\n",p->index,p->stu_name,p->stu_score);
p = p->next;
}
fclose(fp);
return;
}
void main()
{
stu_info *head;
/* create the list for stu_info */
head = stu_info_list_create();
if(head == NULL) return;
//show the stu_info have input
stu_info_list_show(head);
/* write the stu_info to the file */
write_stu_info_to_file(head);
stu_info_list_show_index(head,4);
destroy_list(head);
}
- 单双链表代码
- 代码
- 代码!
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- 代码
- Twitter Storm源代码分析之acker工作流程
- hdu2413 二分+二分匹配
- 使用 RSpec + Capybara 简单 BDD 入门 -目录
- 一款全新的概念货车WAVE
- 第五周项目三-对象数组操作长方体类
- 单双链表代码
- 相关分析与回归分析变量选择方法(笔记)
- vs2010+win7编译log4cxx顺便解决输出问号乱码
- hdu 2191 悼念512汶川大地震遇难同胞——珍惜现在,感恩生活(dp多重背包)
- 5-1-1-三角形类的构造函数
- XCode5显示行号
- MifareUltralight官方文档翻译
- AutoCompleteTextView零输入自动提示
- Jni学习之访问字段