数据结构---链表

数据结构:

四种逻辑结构:集合结构、线性结构、树型结构、图形结构

四种存储结构:顺序存储、链式存储、索引存储、散列存储

链表在数据结构中是个难点也是重点

在链表中一个数据元素由两部分构成,数据内容与关系。

关系:通过指针元素来表示,存储下一个数据元素的地址

下面是关于链表的操作:

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


struct node
{

    char name[20];
    struct node * next;

};

struct node *  create()
{

    struct node * head = malloc(sizeof(struct node));

    if(head == NULL)
    {
        printf("malloc linklist error\n");
        return NULL;
    }

    head->next = NULL;

    return head;
}

void insert_list(struct node * head,int pos,char * data)
{

    struct node * p = head;
    int i = 0;
    while(i < pos - 1 && p->next != NULL)
    {
        p = p->next;
        i++;
    }
    struct node * new = malloc(sizeof(struct node ));

    new->next = p->next;
    p->next = new;
    strcpy(new->name,data);

}

int empty(struct node * head)
{

    if(head->next == NULL)
    {
        printf("is empty\n");
        return 1;
    }
    
    return 0;
}

void delete_list(struct node * head,int pos)
{    

    if(empty(head))
    {
        return ;
    }

    struct node * p = head;

    int i = 0;
    while(i < pos - 1 && p->next != NULL)
    {
        p = p->next;
        i++;
    }

    if(p->next == NULL)
    {
        printf("pos is not found\n");
        return ;
    }

    struct node * q = p->next;
    p->next = q->next;

    printf("delete data is : %s\n",q->name);
    free(q);

}

void update_pos_list(struct node * head,int pos ,char * newdata)
{

    struct node * p = head;
    int i = 0;
    while(i < pos)
    {
        p = p->next;
        if(p == NULL)
        {
            printf("error pos\n");
            return ;
        }
        i++;
    }
    
    strcpy(p->name,newdata);
}

void update_data_list(struct node * head,char * data,char * newdata)
{
    /*
    struct node * p = head->next;

    while(p != NULL)
    {
        if( strcmp(p->name,data) == 0 )
        {
            strcpy(p->name,newdata);
        }
        p = p->next;
    }
    */

    struct node * p = head;

    while(p->next != NULL)
    {
    
        p = p->next;
        if( strcmp(p->name,data) == 0 )
        {
            strcpy(p->name,newdata);
        }
    }
}

void show_list(struct node * head)
{
    struct node * p = head;

    while(p->next != NULL)
    {
        p = p->next;
        printf("%s\t",p->name);
    }
    printf("\n");
}

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值