栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

数据结构之链表的实际应用

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

数据结构之链表的实际应用

今天给大家分享链表的实际应用,很多人学数据结构时总是学课本里的伪代码,其实看懂了课本的伪代码只是学会了数据结构的20%,在学数据结构时我们应该要多注重实践,多用已学的数据结构来解决一些实践的问题。下面是我自己写的链表完整代码(包括创建链表,链表插入数据,链表删除数据,修改链表指定位置的值),以供大家参考。

完整源代码:

#include  
#include
struct Stu *create(int n);
void print(struct Stu *head);
void deleteNode(struct Stu *head,int n);
void insertNode(struct Stu *head,int n);
void change(struct Stu *head,int n);
struct Stu{//定义结构体 
    int id;
    char name[50];
    struct Stu *next;
};
int main(){
    int n,j,in,cn;
    char c;
    struct Stu *head = NULL;   //创建头指针 
    printf("请输入你想要创建的节点个数:n");
    scanf("%d",&n);
    head = create(n);
    print(head);
    while(true){
    printf("请选择你想要执行的操作:n");
    printf("1.插入节点n2.删除节点n3.修改节点n4.退出程序n");
    scanf(" %c",&c);
    if(c =='1'){
    printf("你想要在哪插入节点:n");
    scanf("%d",&in);
    insertNode(head,in);
    print(head); 
    }else if(c == '2'){
    printf("你想要删除哪个节点的数据:n");
    scanf("%d",&j);
    deleteNode(head,j);
    print(head);
    }else if(c =='3'){
    printf("你想要修改哪个节点的数据:n");
    scanf("%d",&cn);
    change(head,cn);
    print(head); 
    }else if(c =='4'){
        exit(0);
    }         
 } 
}
struct Stu *create(int n){
    struct Stu *head,*node,*end;                           //定义头节点,普通节点,尾节点 
    head = (struct Stu *)malloc(sizeof(struct Stu));     //给头节点申请内存 
    //head->id = n;                                        //头节点的数据域保存链表的长度 
    end = head;                                            //若是空表,则头尾地址一致 
    for(int i=0;i         node = (struct Stu *)malloc(sizeof(struct Stu));//给普通节点申请内存空间 
        scanf("%d %s",&node->id,node->name);            //给数据域赋值 
        end->next = node;                                //让上一个节点的数据域指向当前节点 
        end = node;                                     //end指向当前节点,最终end指向尾节点 ,end指针向后移动 
    }
    end->next = NULL;                                   //给end的指针域置空 
    return head;                                        //返回头节点的地址 
}
void print(struct Stu *head){
    struct Stu *p = head;
    int j =1;
    p = p->next;  //不打印头节点的数据域中的值 
    while(p != NULL){
        printf("%dt%dt%sn",j,p->id,p->name);//遍历整个链表,给整个链表赋值 
        p = p->next;
        j++;
    }
}
void deleteNode(struct Stu *head,int n){         //删除n处的节点 
    struct  Stu *p = head,*pr = head;
    int i =0;
    while(i         pr = p;
        p = p->next;
        i++;
    }
    if(p!=NULL){
        pr->next = p->next;
        free(p);
    } else{
        printf("节点不存在!n"); 
    }

void insertNode(struct Stu *head,int n){    //插入节点 
    struct Stu *p = head,*pr;
    pr = (struct Stu*)malloc(sizeof(struct Stu));  //让pr指向新建节点申请的内存 
    printf("input data:n");
    scanf("%d %s",&pr->id,pr->name);
    int i = 0;
    //当插入位置是尾节点时,只要在尾节点后再插入一个节点,并让尾节点的指针域指向新建节点,新建节点的指针域置空
    while(i         p = p->next;
        i++;
    }
    if(p!=NULL){            //如果p没越界 
        pr->next = p->next; //将新建节点的地址指向将要插入节点的后一个节点的地址 。pr在p之后。 
        p->next = pr;       //使插入节点指向新建节点 
    }else{
        printf("节点不存在!n");
    }
}
void change(struct Stu *head,int n){
    struct Stu *p = head;
    int i = 0;
    while(i         p = p->next;
        i++;
    }
    if(p != NULL){             
    printf("请输入修改之后的值:n");
    scanf("%d %s",&p->id,p->name);    
    }else{
        printf("节点不存在!n");
    }      

 
执行程序效果图:

本贴为博主亲手整理。如有错误,请评论区指出,一起进步。谢谢大家的浏览。

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/779527.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号