C语言链表的输入输出
2018-01-28 18:15
1056 查看
1.学生成绩的输入输出
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(student)
typedef struct students
{
char name[20];
int num;
float score;
struct students *next;
}student;
int n;
int main()
{
student *lianbiao(void);
void print(student *p);
student *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
student *lianbiao(void)
{
student *head, *p1, *p2;
n = 0;
p1 = p2 = (student*) malloc(len);
head = NULL;
scanf_s("%s %d %f", p1->name,20,&p1->num, &p1->score);
while (p1->num != 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 =(student*) malloc(len);
scanf_s("%s %d %f", p1->name, 20,&p1->num, &p1->score);
}
p2->next = NULL;
return head;
}
void print(student *head)
{
student *p=head;
printf("Having %d student:\n", n);
if (head != NULL)
{
printf("Name number score\n");
while (p != NULL)
{
printf("%-10s%-8d%-5.2f\n", p->name, p->num, p->score);
p = p->next;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(preson)
union clas_position
{
int clas;
char position[20];
};
typedef struct presons
{
int num;
char name[20];
char sex;
char job[20];
union clas_position category;
struct presons *next;
}preson;
int n;
int main()
{
preson *lianbiao(void);
void print(preson *head);
preson *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
preson *lianbiao(void)
{
preson *head, *p1, *p2;
n = 0;
p1 = p2 = (preson*)malloc(len);
head = NULL;
scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
while (p1->num!= 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (preson*)malloc(len);
scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
}
p2->next = NULL;
return head;
}
void print(preson *head)
{
preson *p = head;
printf("Having %d persons:\n", n);
if (head != NULL)
{
printf("NO. name sex job class/position\n");
while (p != NULL)
{
printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
if (p->job[0]== 's' || p->job[0] == 'S')
printf("%-8d\n", p->category.clas);
else if (p->job[0] == 't' || p->job[0] == 'T')
printf("%-8s\n", p->category.position);
p = p->next;
}
}
}
2.学生与老师信息的输入(共用体)
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(preson)
union clas_position
{
int clas;
char position[20];
};
typedef struct presons
{
int num;
char name[20];
char sex;
char job[20];
union clas_position category;
struct presons *next;
}preson;
int n;
int main()
{
preson *lianbiao(void);
void print(preson *head);
preson *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
preson *lianbiao(void)
{
preson *head, *p1, *p2;
n = 0;
p1 = p2 = (preson*)malloc(len);
head = NULL;
scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
while (p1->num!= 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (preson*)malloc(len);
scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
}
p2->next = NULL;
return head;
}
void print(preson *head)
{
preson *p = head;
printf("Having %d persons:\n", n);
if (head != NULL)
{
printf("NO. name sex job class/position\n");
while (p != NULL)
{
printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
if (p->job[0]== 's' || p->job[0] == 'S')
printf("%-8d\n", p->category.clas);
else if (p->job[0] == 't' || p->job[0] == 'T')
printf("%-8s\n", p->category.position);
p = p->next;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(student)
typedef struct students
{
char name[20];
int num;
float score;
struct students *next;
}student;
int n;
int main()
{
student *lianbiao(void);
void print(student *p);
student *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
student *lianbiao(void)
{
student *head, *p1, *p2;
n = 0;
p1 = p2 = (student*) malloc(len);
head = NULL;
scanf_s("%s %d %f", p1->name,20,&p1->num, &p1->score);
while (p1->num != 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 =(student*) malloc(len);
scanf_s("%s %d %f", p1->name, 20,&p1->num, &p1->score);
}
p2->next = NULL;
return head;
}
void print(student *head)
{
student *p=head;
printf("Having %d student:\n", n);
if (head != NULL)
{
printf("Name number score\n");
while (p != NULL)
{
printf("%-10s%-8d%-5.2f\n", p->name, p->num, p->score);
p = p->next;
}
}
}
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(preson)
union clas_position
{
int clas;
char position[20];
};
typedef struct presons
{
int num;
char name[20];
char sex;
char job[20];
union clas_position category;
struct presons *next;
}preson;
int n;
int main()
{
preson *lianbiao(void);
void print(preson *head);
preson *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
preson *lianbiao(void)
{
preson *head, *p1, *p2;
n = 0;
p1 = p2 = (preson*)malloc(len);
head = NULL;
scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
while (p1->num!= 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (preson*)malloc(len);
scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
}
p2->next = NULL;
return head;
}
void print(preson *head)
{
preson *p = head;
printf("Having %d persons:\n", n);
if (head != NULL)
{
printf("NO. name sex job class/position\n");
while (p != NULL)
{
printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
if (p->job[0]== 's' || p->job[0] == 'S')
printf("%-8d\n", p->category.clas);
else if (p->job[0] == 't' || p->job[0] == 'T')
printf("%-8s\n", p->category.position);
p = p->next;
}
}
}
2.学生与老师信息的输入(共用体)
#include <stdio.h>
#include <stdlib.h>
#define len sizeof(preson)
union clas_position
{
int clas;
char position[20];
};
typedef struct presons
{
int num;
char name[20];
char sex;
char job[20];
union clas_position category;
struct presons *next;
}preson;
int n;
int main()
{
preson *lianbiao(void);
void print(preson *head);
preson *head;
head = lianbiao();
print(head);
system("pause");
return 0;
}
preson *lianbiao(void)
{
preson *head, *p1, *p2;
n = 0;
p1 = p2 = (preson*)malloc(len);
head = NULL;
scanf_s("%d %s %c", &p1->num, p1->name, 20, &p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
while (p1->num!= 0)
{
n = n + 1;
if (n == 1) head = p1;
else p2->next = p1;
p2 = p1;
p1 = (preson*)malloc(len);
scanf_s("%d %s %c", &p1->num, p1->name, 20,&p1->sex);
scanf_s("%s", p1->job,20);
if (p1->job[0] == 's' || p1->job[0] == 'S')
scanf_s("%d", &p1->category.clas);
else if (p1->job[0] == 't' || p1->job[0] == 'T')
scanf_s("%s", p1->category.position, 20);
else printf("input error!");
}
p2->next = NULL;
return head;
}
void print(preson *head)
{
preson *p = head;
printf("Having %d persons:\n", n);
if (head != NULL)
{
printf("NO. name sex job class/position\n");
while (p != NULL)
{
printf("%-10d %-10s %-4c %-10s", p->num, p->name, p->sex, p->job);
if (p->job[0]== 's' || p->job[0] == 'S')
printf("%-8d\n", p->category.clas);
else if (p->job[0] == 't' || p->job[0] == 'T')
printf("%-8s\n", p->category.position);
p = p->next;
}
}
}
相关文章推荐
- C语言中,链表的创建、输入、输出、链接。
- 单链表的输入和输出(c语言编写)
- 【C语言】:字符串替换空格,输入“we are happy!”,输出”we%20are%20happy!“
- 黑马程序员—C语言输入与输出
- [原]C语言实现双向链表删除节点、插入节点、双向输出等操作
- 学习笔记——C语言实现单链表的基本操作:创建、输出、插入结点、删除结点、逆序链表
- ACM入门------C语言中如何实现多组数据输入输出
- C语言输入一个数,倒序输出
- C语言创建单链表,输出单链表的内容。
- C语言的基本输入与输出函数(全解)
- hdu2044 递推,主要是关于c语言的输入输出
- C语言输入系数a,b,c,输出一元二次方程的根
- 输入一个链表,输出该链表中倒数第k个结点。
- 输入两个单调递增的链表,输出两个链表合成后的链表,当然我们需要合成后的链表满足单调不减规则。
- 输入和输出的总结(c语言)
- C语言输入、输出格式控制
- 【C语言】编写一个程序从标准输入读取字符,并把他们写到标准输出。除了大写字母转换成小写字母之外,其他的原样输出。
- 【C语言】从标准输入读取字符,所有非字母字符完全按照输入形式输出,字母字符在输出前加密
- c语言 这里gets_s读取字符,输入A,能输出a[0]为A,而输入AB时,a[0]没有任何输出,什么原因?
- 输入一个链表,输出该链表中倒数第k个结点