美文网首页
linux gcc 7.3.0 实现 c单向链表

linux gcc 7.3.0 实现 c单向链表

作者: 纵春水东流 | 来源:发表于2019-05-03 15:01 被阅读0次

1、核心

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct stu{
    char name[20];
    char num[20];
    struct stu *next;
};
struct stu *CreateList(){
    struct stu *p,*next,*head;  
    char name1[20];
    char num1[20];
    int n=0;
    while('q' !=name1[0]){
        n++;
        if(1==n) {
            head=(struct stu *)malloc(sizeof(struct stu));
            p=head;
            printf("%d号学生的名字:(q回到菜单)",n);
            scanf("%s",name1);getchar();
            if('q' == name1[0] && '\0' == name1[1]) 
                return head;
            printf("%d号学生的电话号码:",n);
            scanf("%s",num1);getchar();
                    strcpy(head->name,name1);
                    strcpy(head->num,num1);
            continue;
            }
            printf("%d号学生的名字:(q回到菜单)",n);
        scanf("%s",name1);getchar();
        if('q' == name1[0] && '\0' == name1[1]) 
            return head;
        printf("%d号学生的电话号码:",n);
        scanf("%s",num1);getchar();

            next=(struct stu *)malloc(sizeof(struct stu));      
        strcpy(next->name,name1);
        strcpy(next->num,num1);
        p->next=next;
        p=next;
    }
    p->next=NULL;
    return head;
}//创建链表
//输出链表
void PrintList(struct stu *head){       
    struct stu *p;  
    int pos=1;
        p=head;
    if(NULL == p){
        printf("当前表中无数据\n");
        return;
    }
    printf("\n序号\t姓名\t学生:\n");
    while(p){
        printf("%d\t%s\t%s\n",pos++,p->name,p->num);
        p = p->next;
    }
}//输出链表
int  main(){
    struct stu *p;
    p=CreateList();
    PrintList(p);
}

2、全部的样子

#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
#include <string.h>
struct stu{
    char name[20];
    char num[20];
    struct stu *next;
};
struct stu *CreateList(){
    struct stu *p,*next,*head;  
    char name1[20];
    char num1[20];
    int n=0;
    while('q' !=name1[0]){
        n++;
        if(1==n) {
            head=(struct stu *)malloc(sizeof(struct stu));
            p=head;
            printf("%d号学生的名字:(q回到菜单)",n);
            scanf("%s",name1);getchar();
            if('q' == name1[0] && '\0' == name1[1]) 
                return head;
            printf("%d号学生的电话号码:",n);
            scanf("%s",num1);getchar();
                    strcpy(head->name,name1);
                    strcpy(head->num,num1);
            continue;
            }
            printf("%d号学生的名字:(q回到菜单)",n);
        scanf("%s",name1);getchar();
        if('q' == name1[0] && '\0' == name1[1]) 
            return head;
        printf("%d号学生的电话号码:",n);
        scanf("%s",num1);getchar();

            next=(struct stu *)malloc(sizeof(struct stu));      
        strcpy(next->name,name1);
        strcpy(next->num,num1);
        p->next=next;
        p=next;
    }
    p->next=NULL;
    return head;
}//创建链表

//输出链表
void PrintList(struct stu *head){       
    struct stu *p;  
    int pos=1;
        p=head;
    if(NULL == p){
        printf("当前表中无数据\n");
        return;
    }
    printf("\n序号\t姓名\t学生:\n");
    while(p){
        printf("%d\t%s\t%s\n",pos++,p->name,p->num);
        p = p->next;
    }
}//输出链表
//扩展部分
//插入
struct stu * ListInsert(struct stu *head,int pos){
    struct stu *p,*insert;  
    int i;
        p=head;
    char name1[20];
    char num1[20];
    if(NULL == p){
        printf("链表为空,需要重新建表\n");
        p=CreateList();
        return p;
    }
    printf("请输入学生的名字:");
    scanf("%s",name1);getchar();
    printf("请输入学生电话号码:");
    scanf("%s",num1);getchar();
    insert=(struct stu *)malloc(sizeof(struct stu));
    strcpy(insert->name,name1);
    strcpy(insert->num,num1);
    if(0 == pos){//处理第一个前插入
        insert->next=head;
        head=insert;
        return head;
    }
    for(i=0;i<pos-1;i++){//p在pos处
        p=p->next;
    }
    if(NULL == p) {
        printf("数据位置超出\n");
        return head;
    }

    insert->next=p->next;
    p->next=insert;
    return head;
}
struct stu *ListDelete(struct stu *head,int pos){//删除第几个学生数据
    struct stu *p,*q;   
    int i;
    p=head;
    if(1==pos){
        q=p;
        p=p->next;
        free(q);
        return p;
    }
    for(i=0+2;i<pos;i++){
        p=p->next;
        if(NULL==p->next){
            printf("位置输入错误");
            return head;
        }
    }
    q=p->next;
    if(NULL==q->next){
        p->next=NULL;
        free(q);
        return head;
    }
    p->next=q->next;
    free(q);
    return head;
    
}
//查找
int  main(){
    struct stu *p;
    char chooce;
        int pos;
    p=CreateList();
    PrintList(p);
    printf("\n*************************************\n");
    printf("1、打印链表\n2、插入数据\n");
    printf("3、删除数据\n4、查找数据\n");
    printf("请输入选择<1-2-3-4>(按其他任意键退出):");
    scanf("%c",&chooce);
    getchar();
    while(1){       
        if('1'==chooce){
            PrintList(p);
        }else if('2'==chooce){
            printf("输入插入第几个学生后面\n");
            scanf("%d",&pos);
            getchar();
            p=ListInsert(p,pos);
        }else if('3'==chooce){
            printf("输入删除第几个学生:");
            scanf("%d",&pos);
            getchar();
            p=ListDelete(p,pos);
        }else if('4'==chooce){
            printf("暂不支持\n");   
        }else{
            break;
        }
        printf("\n*************************************\n");
        printf("1、打印链表\n2、插入数据\n");
        printf("3、删除数据\n4、查找数据\n");
        printf("\n*************************************\n");
        printf("请输入选择<1-2-3-4>(按其他任意键退出):");
        scanf("%c",&chooce);
        getchar();          
    }
}

3、截图效果


图片.png

相关文章

网友评论

      本文标题:linux gcc 7.3.0 实现 c单向链表

      本文链接:https://www.haomeiwen.com/subject/satznqtx.html