简单单链表的实现—C语言

本例简单实现了单链表,并附上调试(VS2013)过程,有助于深刻理解

单链表

链表是一种常见的重要的数据结构,它是动态的进行内存存储分配的一种结构。用数组存放数据时,必须事先定义固定的长度(即元素个数),但是事先难以确定有多少个元素时,则必须把数组定义的足够大,以保证成功。无疑,这会造成内存浪费,然而,链表则没有这种缺点,它可以根据需要,动态开辟内存单元。链表中的各个元素在内存中是可以不是连续存放的,但是要找到某一元素,必须知道它的地址,这就需要链表必须有一个头指针(head)。

本例包含
- 单链表的创建
- 单链表的查找
- 单链表的删除
- 单链表的打印
- 单链表的清除

实现代码

node.h

#ifndef _NODE_H_
#define _NODE_H_

typedef struct _node{
    int value;
    struct _node * next;
} Node;

#endif

main.c

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

#include "node.h"

//typedef struct _node{
//  int value;
//  struct _node* next;
//} Node;

typedef struct _list{
    Node* head;
} List;

void list_add(List* pList, int number);
void list_find(List* pList, int number);
void list_delete(List* pList, int number);
void list_print(List* pList);
void list_clear(List* pList);

int main(){

    List list;
    list.head = NULL;

    printf("Please input some number of int: ");
    int number = 0;
    do{
        scanf("%d", &number);
        if (number != -1){
            list_add(&list, number);
        }
    } while (number != -1);

    printf(" this is list your input: ");
    list_print(&list);
    printf("\n");

    printf("Please input one number of int,it will add that list: ");
    int s = 0;
    scanf("%d", &s);
    list_add(&list, s);

    printf(" this is list your input: ");
    list_print(&list);
    printf("\n");

    printf("Please input one number of int,we will find it at your list: ");
    int n = 0;
    scanf("%d", &n);
    list_find(&list, n);

    printf("Please input one number of int,we will delete it at your list: ");
    int m = 0;
    scanf("%d", &m);
    list_delete(&list, m);

    printf(" this is list your input: ");
    list_print(&list);
    printf("\n");

    list_clear(&list);

    printf(" this is list is cleared! ");

    return 0;
}

void list_add(List* pList, int number){
    // add to liked-list
    Node* p = (Node*)malloc(sizeof(Node));
    p->value = number;
    p->next = NULL;

    Node* last = pList->head;
    if (last){
        // find last
        while (last->next){
            last = last->next;
        }
        //attach
        last->next = p;
    }
    else{
        pList->head = p;
    }
}

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

void list_delete(List* pList, int number){
    Node *p,
        *q;
    for (q = NULL, p = pList->head; p; q = p, p = p->next){
        if (p->value == number){
            if (q){
                q->next = p->next;
            }
            else{
                pList->head = p->next;
            }
            free(p);
            break;
        }
    }
}

void list_find(List* pList, int n){
    int isFind = 0;
    Node* p;
    for (p = pList->head; p; p = p->next){
        if (p->value == n){
            printf("Find!\n");
            isFind = 1;
            break;
        }
    }
    if (!isFind){
        printf("Not Find!\n");
    }
}

void list_clear(List* pList){
    Node *p,
        *q;
    for (p = pList->head; p; p = q){
        q = p->next;
        free(p);
    }
}

调试

1、单链表创建完成后如下图,大家可以想想内存上的情况:

init_list

2、充分理解for(;;){}

  • 第一次运行到main.c的127行

for_first

  • 第二次运行到main.c的127行

for_second

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值