C 单向链表的实现及部分功能

马上要做程序设计期末作业 (⋟﹏⋞),恰好在实验手册上看到单向链表,顺便学习下,如有错误欢迎指正。

 

节点结构体定义

struct node
{
    char name[20];
    int age;
    double salary;
    struct node *next;
};

 

链表的建立及增加

struct node *creat(struct node *head,struct node data){
    struct node *p;
    p=(struct node*)malloc(sizeof(struct node));
    data.next=NULL;
    *p=data;
    if(head!=NULL)p->next=head;
    return p;
}

 

链表数据输出

void print(struct node *head){
    if(head==NULL){
        printf("No storage information is available\n");
        return;
    }
    printf("name\tage\tsalary\n");
    while(head!=NULL){
        printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
        head=head->next;
    }
}

 

链表数据查找 这里举例名字

struct node *find(struct node *head,char f_name[20]){
    if(head==NULL){
        printf("This information is not included in the data\n");
        return NULL;
    }
    while(strcmp(head->name,f_name)&&head->next!=NULL)
        head=head->next;
    if(!strcmp(head->name,f_name)){
        printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
        return head;
    }
    printf("This information is not included in the data\n");
    return NULL;
}

 

链表数据删除 这里举例名字

struct node *del(struct node *head,char d_name[20]){
    struct node *p,*q=NULL;
    p=head;
    while(p->next!=NULL&&strcmp(p->name,d_name)){
        q=p;
        p=p->next;
    }
    if(!strcmp(p->name,d_name)){
        if(q==NULL)head=p->next;
        else q->next=p->next;
        free(p);
        printf("The data is deleted\n");
    }
    else printf("The data not been found\n");
    print(head);
    return head;
}

 

链表数据修改

void modification(struct node *head,char m_name[20]){
    struct node *q;
    q=find(head,m_name);
    if(q!=NULL){
        printf("Please enter\nName\tAge\tSalary\n");
        scanf("%s%d%lf",q->name,&q->age,&q->salary);
        print(head);
    }
}

 

完整代码

#include<stdio.h>
#include<string.h>
#include<stdlib.h>

struct node
{
    char name[20];
    int age;
    double salary;
    struct node *next;
};

//链表的建立及增加
struct node *creat(struct node *head,struct node data){
    struct node *p;
    p=(struct node*)malloc(sizeof(struct node));
    data.next=NULL;
    *p=data;
    if(head!=NULL)p->next=head;
    return p;
}

//链表数据输出
void print(struct node *head){
    if(head==NULL){
        printf("No storage information is available\n");
        return;
    }
    printf("name\tage\tsalary\n");
    while(head!=NULL){
        printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
        head=head->next;
    }
}

//链表数据查找 这里举例名字
struct node *find(struct node *head,char f_name[20]){
    if(head==NULL){
        printf("This information is not included in the data\n");
        return NULL;
    }
    while(strcmp(head->name,f_name)&&head->next!=NULL)
        head=head->next;
    if(!strcmp(head->name,f_name)){
        printf("%s\t%d\t%.2lf\n",head->name,head->age,head->salary);
        return head;
    }
    printf("This information is not included in the data\n");
    return NULL;
}

//链表数据删除 这里举例名字
struct node *del(struct node *head,char d_name[20]){
    struct node *p,*q=NULL;
    p=head;
    while(p->next!=NULL&&strcmp(p->name,d_name)){
        q=p;
        p=p->next;
    }
    if(!strcmp(p->name,d_name)){
        if(q==NULL)head=p->next;
        else q->next=p->next;
        free(p);
        printf("The data is deleted\n");
    }
    else printf("The data not been found\n");
    print(head);
    return head;
}

//链表数据修改
void modification(struct node *head,char m_name[20]){
    struct node *q;
    q=find(head,m_name);
    if(q!=NULL){
        printf("Please enter\nName\tAge\tSalary\n");
        scanf("%s%d%lf",q->name,&q->age,&q->salary);
        print(head);
    }
}

int main(){
    int i,n;
    struct node *head=NULL,data;
    char f_name[20],d_name[20],m_name[20];

    printf("Please enter the number of data to be added\n");
    scanf("%d",&n);
    printf("Please enter\nName\tAge\tSalary\n");
    for(i=0;i<n;i++){
        scanf("%s%d%lf",data.name,&data.age,&data.salary);
        head=creat(head,data);
    }
    print(head);

    printf("Please enter the name you want to find\n");
    scanf("%s",f_name);
    find(head,f_name);
    printf("Please enter the name of the employee you want to delete\n");
    scanf("%s",d_name);
    head=del(head,d_name);

    printf("Please enter the name of the information you want to modify\n");
    scanf("%s",m_name);
    modification(head,m_name);
    return 0;
}

 

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值