链表学习笔记

链表是由若干个节点构成的,且链表在内存中的存储位置是不连续的,链表的两个节点之间一般通过一个指针来从一个结点指向另一个结点。

C语言实现不带头结点的单向链表(头插法)

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

typedef struct node
{
    int             data;
    struct node     *next;
}node_t;

int search_node(node_t *head, int x);
void insert(node_t *head, int position, int x);
void delete_node(node_t *head,int x);

int main(int argc, char argv)
{
    node_t      *head = NULL;
    node_t      *node = NULL;
    int         i;

    for(i=0; i<5; i++)
    {
        node = (node_t *)malloc(sizeof(node_t));
        if(node == NULL)
        {
            printf("malloc failure!\n");
            return -1;
        }
        else
        {
            node -> data = i;
            node -> next = NULL;

            if(NULL == head)
            {
                head = node;
                printf("add node[%d]:node data =%d\n", i, i);
            }
            else
            {
                printf("add node[%d]:node data = %d\n", i, i);
                node -> next = head; //头插
                head = node;
            }
        }
     }
     
    travel_node(head);
    search_node(head, 2);
    insert(head,5,11);
    delete_node(head,3);
    travel_node(head);
    destroy_node(head);

    return 0;
}

int travel_node(node_t *head)
{
    node_t *temp = head;
    if(NULL == head)
    {
        printf("travel node failure!\n");
        return -1;
    }
     while(temp != NULL)
    {
        if(temp -> next == NULL)
        {
            printf("node data = %d\n", temp->data);
            printf("travel node finish!\n");
            return 0;
        }
        else
        {
            printf("node data = %d\n", temp->data);
            temp = temp->next;
        }
    }

    return 0;
}

/* 查找元素 */
int search_node(node_t *head, int x)
{
    int count = 0;
    node_t *p = head;
    while(p != NULL)
    {       
        if(p->data == x)
        {   
            count++;
        }   
        p = p -> next;
    }
    printf("count = %d\n", count);

    return 0;
}

/*插入元素*/
void insert(node_t *head, int position, int x)
{
    node_t *p = head;
    node_t *q = NULL;
    int         i;

    for(i=0; i<position-1;i++)      //找到要插入结点的位置的前一个结点
    {
        p = p->next;

    }

    q = (node_t *)malloc(sizeof(node_t));
    q->data = x;
    q->next = p->next;
    p->next = q;

}

/*删除数据域为x的元素*/
void delete_node(node_t *head,int x)
{
    node_t *p = head;
    node_t *pre = head;
    while(p != NULL)
    {
        if(p->data == x)
        {
            pre->next = p->next;
            free(p);
            p = pre->next;
        }
        else
        {
            pre = p;
            p = p->next;

        }
    }
}

/*销毁链表*/
int destroy_node(node_t *head)
{
    node_t *node = head;

    while(node != NULL)
    {
        head = head->next;
        free(node);
        node = head;
    }
    retur

2.不带头结点的单向链表(尾插法)

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

typedef struct node
{
    int     data;
    struct  node *next;
}node_t;
void travel_list(node_t *head);
void destroy_node(node_t *head);
void delete_node(node_t *head, int x);
void insert_node(node_t *head, int position, int x);
void search_node(node_t *head, int x);

int main(int argc, char **argv)
{
    node_t  *head = NULL;
    node_t  *node = NULL;
    node_t  *tail = NULL;
    int         i;

    for(i = 0;i<5;i++)
    {
        node = (node_t *)malloc(sizeof(node_t));
        if(node == NULL)
        {
            printf("malloc failure!\n");
            return -1;
        }
        node->data = i;
        node->next = NULL;
        
        if(head == NULL)
        {
            head = node;
            tail = node;
            printf("add node data:%d\n",node->data);
        }
        else
        {
            tail->next = node;
            tail = node;
            printf("add node data:%d\n",node->data);

        }

    }
      travel_list(head);
    delete_node(head, 2);
    printf("\n");
    travel_list(head);
    insert_node(head,2,8);
    printf("\n");
    travel_list(head);
    destroy_node(head);

    return 0;
}
void travel_list(node_t *head)
{
    node_t *p = head;
    while(p !=  NULL)
    {
        printf("node data: %d\n",p->data);
        p = p->next;
    }

    return ;
}

/* 删除数据域为x的结点 */
void delete_node(node_t *head, int x)
{
    node_t *p = head;
    node_t *pre = head;

    while(p != NULL)
    {
        if(p->data == x)
        {
            pre->next = p->next;
            free(p);
            p = pre->next;
        }
        else
        {
            pre = p;
            p = p->next;
        }
        }
        
    return ;
}
void destroy_node(node_t *head)
{
    node_t *p = head;
    while(p != NULL)
    {
        free(p);
        p=p->next;
    }
    return ;
}

void insert_node(node_t *head, int position, int x)
{
    node_t  *p = head;
    node_t  *new_node = NULL;
    int      i;

    new_node = (node_t *)malloc(sizeof(node_t));

    for(i=0; i<position-1; i++)
    {
        p = p->next;
    }

    new_node->data = x;
    new_node->next = p->next;
    p->next = new_node;

    return ;
    }
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
你好!关于学习数据结构的C语言笔记,我可以给你一些基本的指导和概念。数据结构是计算机科学中非常重要的一门课程,它涉及存储和组织数据的方法。C语言是一种常用的编程语言,很适合用于实现各种数据结构。 下面是一些数据结构的基本概念,你可以在学习笔记中包含它们: 1. 数组(Array):一种线性数据结构,可以存储相同类型的元素。在C语言中,数组是通过索引访问的。 2. 链表(Linked List):也是一种线性数据结构,但不需要连续的内存空间。链表由节点组成,每个节点包含数据和指向下一个节点的指针。 3. 栈(Stack):一种后进先出(LIFO)的数据结构,类似于装满物品的箱子。在C语言中,可以使用数组或链表来实现栈。 4. 队列(Queue):一种先进先出(FIFO)的数据结构,类似于排队等候的队伍。同样可以使用数组或链表来实现队列。 5. 树(Tree):一种非线性数据结构,由节点和边组成。每个节点可以有多个子节点。二叉树是一种特殊的树结构,每个节点最多有两个子节点。 6. 图(Graph):另一种非线性数据结构,由节点和边组成。图可以用来表示各种实际问题,如社交网络和地图。 这只是数据结构中的一些基本概念,还有其他更高级的数据结构,如堆、哈希表和二叉搜索树等。在学习笔记中,你可以介绍每个数据结构的定义、操作以及适合使用它们的场景。 希望这些信息对你有所帮助!如果你有任何进一步的问题,请随时提问。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值