C语言数据结构第二章—— 用链表的实现的线性表

C语言数据结构第二章—— 用可变数组的实现的线性表

这些东西,重要的是节点的定义,他们的连接方式,剩下的就是实现了。

需要注意的点有

  1. 链表的循环
    定义一个结构体指针,从head开始,一直next到最后的NULL,就是常用的链表循环
    for ( p = pList->head; p; p =p->next){
        printf("%d\t", p->value);
  1. 有输入情况的判定方法
if ( number != -1) //如果输入不结束,才进入循环
{
    
}
  1. 定义节点
    定义节点的地址,然后节点内有 值 和 下一个节点的地址,对于节点大家都用地址,但是要定义。
typedef struct node{
    int value;
    struct node *next; //指向下一个同结构类型的指针
}Node;
  1. 定义head Node *head = NULL;
  2. 怎么往链表内增加一个新元素
Node *p = (Node*)malloc(sizeof(Node)); //申请一块新的节点,p是plus的意思
p->value = number;
p->next = NULL;在这里插入代码片
  1. 怎么把之前最后的节点连上最新的节点
    • 找到最后的节点(从第一个往后找)
Node *last = head; //从第一个开始
if ( last ) {      //如果有了第一个节点的话
    while ( last->next ) { //last->next 如果是NULL,那就是最后一个节点
        last = last->next; //如果不是最后一个节点的话,那就让last等于一个节点
    }                      //该函数出来之后就是最后节点了
// 下边还有else

6.2 最后节点和最新的连线

last->next = p;
}
else { //如果第一个节点都没被定义的话
    head = p;
}
  1. 把这个功能从主函数抽出来做函数
    多做一个结构来包含指向head的指针,这样的话就能把head结构的地址传入函数,从而能让函数改变结构体的值。
    保留修改要传该值的指针进函数,*
void add(List *pList, int number);
add(&list, number);
typedef struct _list {
    Node *head;
} List;
  1. 做更多的功能函数,
    • 如打印链表的函数 循环依据是:p =p->next是否为真
    • 链表的清除
for ( p = pList->head; p; p = q){
        q = p->next;
        free(p);
}

C语言代码——实现部分操作

// 2020/2/13
//最简单的链表,最纯粹的享受
//是看浙大的翁凯的MOOC写的,从那入门

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


typedef struct _node {
    int value;
    struct _node *next; //指向下一个同结构类型的指针
} Node;

typedef struct _list {
    Node *head;
} List;

void add(List *pList, int number);
void print(List *pList);
void find(List *pList);
void Delete(List *pList);

int main()
{
    List list;
    list.head = NULL;
    int number; //每做完一种操作print出来检查一下,程序员的好习惯(逃)
    do {
        scanf("%d", &number);
        if( number != -1){
                add(&list, number);
        }
    } while ( number != -1 );
    print(&list);
    find(&list);
    Delete(&list);
    print(&list);
    return 0;
}

void add(List *pList, int number)
{
    // add to linked-list
    Node *p = (Node*)malloc(sizeof(Node));
    p->value = number;
    p->next = NULL;
    // find the last
    Node *last = pList->head;
    if ( last ) {
        while ( last->next ) {
            last = last->next;
        }
        //attach
        last->next = p;
    } else {
        pList->head = p;
    }
}

void print(List *pList){
    Node *p;
    for ( p = pList->head; p; p =p->next){
        printf("%d\t", p->value);
    }
}

void find(List *pList){
    Node *p;
    int isFound = 0, number = 0;
    scanf("%d", &number);
    for ( p = pList->head; p; p=p->next ){
        if ( p->value == number){
            printf("找到了\n");
            isFound = 1;
            break;
        }
    }
    if ( !isFound) {
        printf("没找到\n");
    }
}

void Delete(List *pList)
{
    int Dnumber=0, isFound = 0;
    scanf("%d", &Dnumber);
    Node *it, *beforeit = NULL;
    for ( it = pList->head; it; it = it->next ){
        if( Dnumber == it->value) {
                if(!beforeit) {
                    pList->head = it->next;
                    isFound = 1;
                }
            else{
                    beforeit->next = it->next;
                    isFound = 1;
            }
        }
    beforeit = it;
    }
    if(isFound == 0) { printf("链表没这个值可以删\n"); }
    else{ printf("已删除%d\n", Dnumber); }
}

后记

这个编辑器有点用不惯,不过比那个富文本编辑器好用点…
不想写串和数组的结构啊啊啊啊…不过还是得写吧,都实现一遍才能说学过了,估计得过两天才能写到树了…

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值