链表(单向/双向)

41 篇文章 0 订阅

单向链表

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

typedef struct phone//定义类型phones
{
        int num;
        int price;
        char brand[16];
        struct phone *next;
}phones;

int num = 0;

//创建列表
struct phones *cteate_list(phones **old_output,phones *input)
//创建链表(1,2级指针)

{
        phones *temp;
        temp = *old_output;
        //创建临时temp将2级指针转化为1级指针
        //而且方便结构体取值

        if(*old_output == NULL)
        {
                printf("struct phones is empty...\n");
                temp = input;
                input->next = NULL;

                printf("the phone has been added :%s\n its price is :%d\n\n",temp->brand,temp->price);
        }

        else
        {
                printf("struct is not empty...\n");

                while(temp->next != NULL)
                {
                        printf("current site is not last...\n");
                        temp = temp->next;
                }
                temp->next = input;
                input->next = NULL;

                if(temp->next == NULL)//测试程序
                {
                        printf("temp->next == NULL\n");
                }
                else
                {
                        printf("temp->next == somethings\n");
                }

                printf("the phone has been added :%s\n its price is :%d\n\n",temp->brand,temp->price);
        }
        return temp;
}

//遍历列表
void print_list(phones *in_output)
{
        phones *output = in_output;
        if(output == NULL)
        {
                printf("struct is empty...\n");
        }
        else
        {
                printf("start to print phones...\n");
                while(output != NULL)
                {
                        printf("phones message is:num:%d, brand:%s, price:%d\n\n",output->num,output->brand,output->price);
                        output = output->next;
                }
        }
}

void delete_member(phones **output,int del_num)
{
        phones *former,*later;
        former = later = *output;
        if(*output == NULL)
        {
        printf("nothing to delete...\n");
        }
        else
        {
                while(later->num != del_num && later->next != NULL)
                {
                        former = later;
                        later = later->next;
                }
                if(later->num == num)
                {
                        if(later == *output)
                        {
                                *output = later->next;
                        }
                        else
                        {
                                former->next = later->next;
                        }
                }
                else
                {
                printf("no such num to delete...\n");
                }
        }
}

void seek_member(phones **output,int s_num)
{
        phones *temp = *output;

        if(*output == NULL)
        {
                printf("nothing to seek...\n");
        }
        else
        {
                while(temp != NULL)
                {
                        if(temp->num == s_num)
                        {
                                printf("seek successfully,num:%d, price:%d, brand:%s\n",temp->num,temp->price,temp->brand);
                                break;
                        }
                        else
                        {
                                printf("no such num...\n");
                                break;
                        }
                }
        }
}
int main()
{
        phones *input = NULL,*output = NULL; //定义输入,输出

        input = (struct phone *)malloc(sizeof(struct phone));
        input->price = 3;
        strcpy(input->brand,"vivo");
        output = cteate_list(&output,input);
        //注意:第一次将链表传出来供后面使用    

        for(int i = 0;i<3;i++)//下面为手动加入
        {
        input = (struct phone *)malloc(sizeof(struct phone));
                //每次加入元素时都要开辟空间

                num++;
                input->num = num;
                scanf("%d %s",&input->price,input->brand);

                cteate_list(&output,input);
        }

        print_list(output);

        while(1)
        {
                int s_num;
                printf("input seek s_num:");
                scanf("%d",&s_num);
                seek_member(&output,s_num);
                if(s_num == 9)
                {
                        break;
                }
                }

        while(1)
        {
                int d_num;
                printf("input delete d_num:");
                scanf("%d",&d_num);
                delete_member(&output,d_num);

                print_list(output);
                if(output == NULL)
                {
                        break;
                }
        }


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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值