C语言链表的基础知识就不多说了,关键在于要知道它是不连续的内存单元通过指针的方式把数据串接成链的。百度一大把资料。
直接对程序加以说明,以及注意事项:
1.添加信息:直接在创建的链表尾部添加。读者也可以自己试着修改成,链表指定位置添加。
2.删除信息:首先分三种情况,第一:删除的数据在表头,第二:删除的数据在表中间,最后,删除的数据是表尾(即next指向的是NULL)。
3.修改、查询信息:通过指定的方式去查找,比较简单没啥好说的。
4.登录:这里我设置了默认的账号密码,三次之内输入正确即可进入创建链表,否则退出进程。
思考:程序45行getchar()的作用?
在scanf中输入name信息后按回车结束,输入缓冲中保存了这个回车符,如果后面一个输入信息格式是字符(串)格式,
它会自动把这个回车符赋给这个信息,所以getchar的作用是吃掉这个回车符。
单片机源程序如下:
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include "student.h"
- int main(int argv,char *argc[])
- {
- Stu *head;
- int entry,Num;
- entry=login();
- if(entry != 0)
- return -1;
- head=creat();
- print(head);
- while(1){
- printf("input 1.添加 2.删除 3.修改 4.查询 5.打印 6.退出\n");
- scanf("%d",&Num);
- switch(Num){
- case 1:head=add(head);print(head);break;
- case 2:head=delet(head);print(head);break;
- case 3:head=modify(head);print(head);break;
- case 4:query(head);break;
- case 5:print(head);break;
- case 6:return 0;
- default:printf("Invalid input\n");break;
- }
- }
- return 0;
- }
- /* 创建学生链表 */
- Stu *creat(void)
- {
- int n=0;
- char i[4];
- Stu *p1,*p2;
- Stu *head = NULL;
- p1=p2=(Stu *) malloc(sizeof(Stu));
- do{
- printf("please input student info:\n");
- printf("name:");
- scanf("%s",p1->name);
- getchar();
- printf("sex:");
- scanf("%c",&p1->sex);
- printf("num:");
- scanf("%d",&p1->num);
- printf("score:");
- scanf("%f",&p1->score);
- printf("addr:");
- scanf("%s",p1->addr);
- if(!n){
- n++;
- head=p1;
- }else{
- p2->next=p1;
- p2=p1;
- }
- printf("whether input next:yes/no\n");
- scanf("%s",i);
- if(!strncmp(i,"y",1)){
- p1=(Stu *)malloc(sizeof(Stu));
- }else{
- p1->next = NULL;
- }
- }while(!strncmp(i,"y",1));
- return head;
- }
- /* 添加学生信息 */
- Stu *add(Stu *p)
- {
- Stu *q,*head;
- head = p;
- char i[4];
- while(p->next != NULL){
- p=p->next;
- }
- q=(Stu *) malloc(sizeof(Stu));
- do{
- printf("please add student info:\n");
- printf("name:");
- scanf("%s",q->name);
- getchar();
- printf("sex:");
- scanf("%c",&q->sex);
- printf("num:");
- scanf("%d",&q->num);
- printf("score:");
- scanf("%f",&q->score);
- printf("addr:");
- scanf("%s",q->addr);
- p->next = q;
- p=q;
- printf("whether add next:yes/no\n");
- scanf("%s",i);
- if(!strncmp(i,"y",1)){
- q=(Stu *) malloc(sizeof(Stu));
- }else{
- q->next= NULL;
- }
- }while(!strncmp(i,"y",1));
- return head;
- }
- /* 删除学生信息 */
- Stu *delet(Stu *p)
- {
- Stu *q,*head;
- int Num;
- char i[4];
- head = q= p;
- if(!p)
- return head;
- do{
- printf("please input delete student num:");
- scanf("%d",&Num);
- while(p->num !=Num){
- if(p->next!=NULL){
- if(q!=p){
- q->next=p;
- q=p;
- }
- p=p->next;
- }else{
- printf("No find the student,sorry!\n");
- return head;
- }
- }
- if(p==head){
- head=p->next;
- }else if(p->next ==NULL){
- q->next=NULL;
- }else{
- q->next=p->next;
- }
- free(p);
- printf("whether delete again:yes/no?\n");
- scanf("%s",i);
- }while(!strncmp(i,"y",1));
- return head;
- }
- /* 修改学生信息 */
- Stu *modify(Stu *p)
- {
- Stu *head;
- char i[4];
- int Num,y;
- head=p;
- printf("please modify student num:");
- scanf("%d",&Num);
- do{
- while(p->num!= Num){
- if(p->next!=NULL){
- p=p->next;
- }else{
- printf("No find the student,sorry!\n");
- return head;
- }
- }
- printf("Do you want to modify 1.name 2.num 3.sex 4.score 5.addr?");
- scanf("%d",&y);
- switch(y){
- case 1:printf("name:"); scanf("%s",p->name);break;
- case 2:printf("num:"); scanf("%d",&p->num);break;
- case 3:printf("sex:"); scanf("%c",&p->sex);break;
- case 4:printf("score:"); scanf("%f",&p->score);break;
- case 5:printf("addr:"); scanf("%s",p->addr);break;
- default:printf("input error!\n");break;
- }
- printf("whether modify again:yes/no?\n");
- scanf("%s",i);
- }while(!strncmp(i,"y",1));
- return head;
- }
- /* 查询学生信息 */
- void query(Stu *p)
- {
- int Num;
- printf("Please input student num:");
- scanf("%d",&Num);
- while(p->num != Num){
- if(p->next == NULL){
- printf("No find the student ,sorry!\n");
- exit(-1);
- }
- p= p->next;
- }
- printf("name=%s ,sex=%c ,score=%4.2f ,addr=%s\n",p->name,p->sex,p->score,p->addr);
- }
- /* 账号登录 */
- int login(void)
- {
- int i=0;
- char wel[]="Welcome to entery the student system!";
- int len=sizeof(wel);
- char account_new[16],account[]="class_student";
- char passwd_new[16],passwd[]="123456";
- printf("defaule:: 账号:class_student 密码:123456\n");
- printf("if error 3 times exit\n");
- for(i=0;i<3;i++){
- printf("please input your 账号:");
- scanf("%s",account_new);
- printf("please input your 密码:");
- scanf("%s",passwd_new);
- if((!strncmp(account,account_new,strlen(account))) &&
- (!strncmp(passwd,passwd_new,strlen(passwd)))) {
- printf("succeeful!\n");
- break;
- }else{
- if(i==2)
- return -1;
- printf("error!\n");
- }
- }
- printf("%s",wel);
- printf("\n");
- return 0;
- }
- /* 信息打印 */
- void print(Stu *p)
- {
- if(!p)
- return;
- do{
- printf("name:%s ,sex:%c ,num:%d , score:%4.2f ,addr:%s \n",p->name,p->sex,p->num,p->score,p->addr);
- p=p->next;
- }while(p != NULL);
- }
复制代码
所有资料51hei提供下载:
c语言之单链表.zip
(7.48 KB, 下载次数: 18)
|