单链表(创建,增,删,查,改,打印,销毁链表)

一、单链表(增,删,查,改,打印,销毁链表)
注意链表头的动态,需要改变头指针的先把指针保存起来
1,链表的创建。
2,链表的打印。(循环打印,遇到null则停止)
3,单链表的插入(包括头插,尾插,随机插)。
提示:①空链表,则直接插入;②只有一个节点和尾插,注意插入后 next=NULL;③其他情况就是正常插入。
3,单链表的删除(包括头删,尾删,随机删)。
提示:①空链表,直接返回NULL;②尾删时要注意把删除的前一个节点next=NULL;③其他情况都是正常删除。
4,单链表的查找(找到返回链表地址)。
提示:正常遍历查找,找到则返回地址,否则返回NULL.
5,单链表的修改(任意位置修改)。
提示:①先找到要修改节点的地址;②除去空的情况,其他的都是正常修改。
二、各函数代码:
1、链表的创建。

SListNode* BuySListNode(DataType x)  //创建新节点
{
    SListNode* tmp;
    tmp=(SListNode*)malloc(sizeof(SListNode));
    if (NULL==tmp)
        return NULL;
    else
    {
        tmp->_data=x;
        tmp->_next=NULL;
        return tmp;
    }
}

2、链表的打印

void SListPrint(SListNode* pHead)  //打印链表
{
    if (NULL==pHead)
    {
        printf("空链表\n");
    }
    else
    {
        while (pHead)
        {
            printf("%d ",pHead->_data);
            pHead=pHead->_next;
        }
        printf("\n");
    }
}

3、链表的插入(①头插;②尾插;③随机插)
①头插

void SListPushFront(SListNode** ppHead, DataType x)  //头插
{
    SListNode *newNode;
    newNode=BuySListNode(x);
    if (NULL==*ppHead)
    {
        newNode->_next=NULL;
        *ppHead=newNode;
    }
    else
    {
        newNode->_next=*ppHead;
        *ppHead=newNode;
    }
}

②尾插

void SListPushBack(SListNode** ppHead, DataType x)  //尾插
{
    SListNode*newNode;
    SListNode*tmp;
    tmp=*ppHead;
    newNode=BuySListNode(x);
    if (NULL==*ppHead)
    {
        *ppHead=newNode;
       (*ppHead)->_next=NULL;
    }
    else
    {
        while (tmp->_next)
            tmp=tmp->_next;

        newNode->_next=NULL;
        tmp->_next=newNode;
    }
}

③随机插入

void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)  //随机插入 
 {
     SListNode*newNode;
     SListNode*tmp=*ppHead;
     newNode=BuySListNode(x);
     if (*ppHead==pos)
     {
         newNode->_next=*ppHead;
         *ppHead=newNode;
     }
     else
     {
         while (tmp->_next!=pos)
             tmp=tmp->_next;

         newNode->_next=pos;
         tmp->_next=newNode;
     }
 }

4、链表的删除(①头删;②尾删;③随机删除)
①头删

void SListPopFront(SListNode** ppHead)  //头删
{
    SListNode *tmp;
    tmp=*ppHead;
    if (NULL==*ppHead || NULL==(*ppHead)->_next)
    {
        *ppHead=NULL;
        free(*ppHead);
        *ppHead=NULL;
    }
    else
    {
        *ppHead=(*ppHead)->_next;
        free(tmp);
        tmp=NULL;
    }
}

②尾删

void SListPopBack(SListNode** ppHead)  //尾删
{
    SListNode *tmp=*ppHead;
    SListNode *cur;
    if (NULL==*ppHead || NULL==(*ppHead)->_next)
    {
        *ppHead=NULL;
        free(*ppHead);
    }
    else 
    {
        while (tmp->_next)
        {
            cur=tmp;
            tmp=tmp->_next;
        }

        tmp=NULL;
        cur->_next=tmp;
        free(tmp);
    }
}

③随机删除

void SListErase(SListNode** ppHead, SListNode* pos) //随机删除
 {
     SListNode*tmp;
     tmp=*ppHead;
     if (*ppHead==pos)
     {
         *ppHead=(*ppHead)->_next;
         free(tmp);
         tmp=NULL;
     }
     else
     {
         while (tmp->_next!=pos)
             tmp=tmp->_next;

         tmp->_next=pos->_next;
         free(pos);
         pos=NULL;
     }
 }

5、链表的查找

SListNode* SListFind(SListNode* pHead, DataType x)  //查找
{
    SListNode*tmp;
    tmp=pHead;
    if (NULL==tmp)
        return NULL;
    else
    {
       do
        {
            if (x==tmp->_data)
                return tmp;

            tmp=tmp->_next;
        }while(NULL!=tmp);
        return NULL;
    }
}

6、链表的修改

void SListChange(SListNode**pphead,SListNode *pos,DataType x)  //链表的修改
 {
     SListNode *tmp;
     tmp=*pphead;
     if (NULL==*pphead)
         return;
     else if (NULL==tmp->_next)
         tmp->_data=x;
     else
     {
         while (pos->_next!= tmp->_next)
            tmp=tmp->_next;

         tmp->_data=x;
     }
 }

7、链表的销毁

void SListDestory(SListNode** ppHead)  //销毁链表
 {
     free(*ppHead);
     *ppHead=NULL;
 }

三、整体代码(后附带测试代码和测试运行结果)
1、SListNode.h

#ifndef __SLISTNODE_H__
#define __SLISTNODE_H__

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

typedef int DataType;

typedef struct SListNode 
{ 
    struct SListNode* _next; 
    DataType _data; 
}SListNode; 

SListNode* BuySListNode(DataType x); //创建新节点 
void SListPrint(SListNode* pHead);  //打印链表
void SListDestory(SListNode** ppHead);  //销毁链表
void SListPushFront(SListNode** ppHead, DataType x); //头插
void SListPushBack(SListNode** ppHead, DataType x); //尾插
void SListPopBack(SListNode** ppHead); //尾删
void SListPopFront(SListNode** ppHead);  //头删
SListNode* SListFind(SListNode* pHead, DataType x); //查找
void SListInsest(SListNode** ppHead, SListNode* pos, DataType x);  //插入 
void SListChange(SListNode**pphead,SListNode *pos,DataType x);  //链表的修改
void SListErase(SListNode** ppHead, SListNode* pos); //清空链表

#endif //__SLISTNODE_H__

2、SListNode.c

#include "SListNode.h"

SListNode* BuySListNode(DataType x)  //创建新节点
{
    SListNode* tmp;
    tmp=(SListNode*)malloc(sizeof(SListNode));
    if (NULL==tmp)
        return NULL;
    else
    {
        tmp->_data=x;
        tmp->_next=NULL;
        return tmp;
    }
}

void SListPrint(SListNode* pHead)  //打印链表
{
    if (NULL==pHead)
    {
        printf("空链表\n");
    }
    else
    {
        while (pHead)
        {
            printf("%d ",pHead->_data);
            pHead=pHead->_next;
        }
        printf("\n");
    }
}


void SListPushFront(SListNode** ppHead, DataType x)  //头插
{
    SListNode *newNode;
    newNode=BuySListNode(x);
    if (NULL==*ppHead)
    {
        newNode->_next=NULL;
        *ppHead=newNode;
    }
    else
    {
        newNode->_next=*ppHead;
        *ppHead=newNode;
    }
}

void SListPushBack(SListNode** ppHead, DataType x)  //尾插
{
    SListNode*newNode;
    SListNode*tmp;
    tmp=*ppHead;
    newNode=BuySListNode(x);
    if (NULL==*ppHead)
    {
        *ppHead=newNode;
       (*ppHead)->_next=NULL;
    }
    else
    {
        while (tmp->_next)
            tmp=tmp->_next;

        newNode->_next=NULL;
        tmp->_next=newNode;
    }
}

void SListPopBack(SListNode** ppHead)  //尾删
{
    SListNode *tmp=*ppHead;
    SListNode *cur;
    if (NULL==*ppHead || NULL==(*ppHead)->_next)
    {
        *ppHead=NULL;
        free(*ppHead);
    }
    else 
    {
        while (tmp->_next)
        {
            cur=tmp;
            tmp=tmp->_next;
        }

        tmp=NULL;
        cur->_next=tmp;
        free(tmp);
    }
}

void SListPopFront(SListNode** ppHead)  //头删
{
    SListNode *tmp;
    tmp=*ppHead;
    if (NULL==*ppHead || NULL==(*ppHead)->_next)
    {
        *ppHead=NULL;
        free(*ppHead);
        *ppHead=NULL;
    }
    else
    {
        *ppHead=(*ppHead)->_next;
        free(tmp);
        tmp=NULL;
    }
}

SListNode* SListFind(SListNode* pHead, DataType x)  //查找
{
    SListNode*tmp;
    tmp=pHead;
    if (NULL==tmp)
        return NULL;
    else
    {
       do
        {
            if (x==tmp->_data)
                return tmp;

            tmp=tmp->_next;
        }while(NULL!=tmp);
        return NULL;
    }
}

 void SListInsest(SListNode** ppHead, SListNode* pos, DataType x)  //随机插入 
 {
     SListNode*newNode;
     SListNode*tmp=*ppHead;
     newNode=BuySListNode(x);
     if (*ppHead==pos)
     {
         newNode->_next=*ppHead;
         *ppHead=newNode;
     }
     else
     {
         while (tmp->_next!=pos)
             tmp=tmp->_next;

         newNode->_next=pos;
         tmp->_next=newNode;
     }
 }

 void SListErase(SListNode** ppHead, SListNode* pos) //随机删除
 {
     SListNode*tmp;
     tmp=*ppHead;
     if (*ppHead==pos)
     {
         *ppHead=(*ppHead)->_next;
         free(tmp);
         tmp=NULL;
     }
     else
     {
         while (tmp->_next!=pos)
             tmp=tmp->_next;

         tmp->_next=pos->_next;
         free(pos);
         pos=NULL;
     }
 }

 void SListChange(SListNode**pphead,SListNode *pos,DataType x)  //链表的修改
 {
     SListNode *tmp;
     tmp=*pphead;
     if (NULL==*pphead)
         return;
     else if (NULL==tmp->_next)
         tmp->_data=x;
     else
     {
         while (pos->_next!= tmp->_next)
            tmp=tmp->_next;

         tmp->_data=x;
     }
 }

 void SListDestory(SListNode** ppHead)  //销毁链表
 {
     free(*ppHead);
     *ppHead=NULL;
 }

3、test.c

#include "SListNode.h"

SListNode *SList;

int main()
{
    SList=BuySListNode(2);  //创建链表    
    SListPrint(SList);  //打印链表

    SListPushFront(&SList,5);  //头插
    SListPushFront(&SList,15);
    SListPrint(SList);

    SListPushBack(&SList,10);  //尾插
    SListPushBack(&SList,1);
    SListPushBack(&SList,4);
    SListPrint(SList);

    SListPopBack(&SList);  //尾删
    SListPrint(SList);

    SListPopFront(&SList);  //头删
    SListPrint(SList);


    printf("%p\n",SListFind(SList,2));  //查找
    printf("%p\n",SListFind(SList,5));
    printf("%p\n",SListFind(SList,10));
    printf("%p\n",SListFind(SList,1));
    printf("%p\n",SListFind(SList,15));


    SListChange(&SList,SListFind(SList,5),56);   //修改
    SListChange(&SList,SListFind(SList,1),46);
    SListChange(&SList,SListFind(SList,2),36);
    SListChange(&SList,SListFind(SList,10),26);
    SListPrint(SList);



    SListErase(&SList,SListFind(SList,56)); //随机删除
    SListErase(&SList,SListFind(SList,36));
    SListPrint(SList);

    SListDestory(&SList);  //销毁链表
    SListPrint(SList);

    return 0;
}

4、测试结果
这里写图片描述

  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
当然,我可以为您提供链表创建销毁加、除和找的代码示例。以下是一个基本的链表结构的定义和相应的操作函数的实现: ```c #include <stdio.h> #include <stdlib.h> // 链表节点结构定义 typedef struct Node { int data; // 数据域 struct Node *next; // 指向下一个节点的指针 } Node; // 创建一个新节点 Node *createNode(int data) { Node *newNode = (Node *)malloc(sizeof(Node)); if (newNode == NULL) { printf("内存分配失败!\n"); exit(1); } newNode->data = data; newNode->next = NULL; return newNode; } // 销毁整个链表 void destroyList(Node *head) { Node *current = head; while (current != NULL) { Node *temp = current; current = current->next; free(temp); } } // 在链表末尾插入一个节点 void insertAtEnd(Node **head, int data) { Node *newNode = createNode(data); if (*head == NULL) { *head = newNode; } else { Node *current = *head; while (current->next != NULL) { current = current->next; } current->next = newNode; } } // 除指定值的节点 void deleteNode(Node **head, int data) { if (*head == NULL) { printf("链表为空!\n"); return; } Node *current = *head; Node *prev = NULL; // 如果头节点就是要除的节点 if (current->data == data) { *head = current->next; free(current); return; } // 遍历链表找要除的节点 while (current != NULL && current->data != data) { prev = current; current = current->next; } // 找到要除的节点,进行除操作 if (current != NULL) { prev->next = current->next; free(current); } else { printf("未找到要除的节点!\n"); } } // 找指定值的节点 Node *searchNode(Node *head, int data) { Node *current = head; while (current != NULL) { if (current->data == data) { return current; } current = current->next; } return NULL; } // 打印链表中的所有节点 void printList(Node *head) { Node *current = head; while (current != NULL) { printf("%d ", current->data); current = current->next; } printf("\n"); } // 测试代码 int main() { Node *head = NULL; // 头节点指针 // 插入节点 insertAtEnd(&head, 1); insertAtEnd(&head, 2); insertAtEnd(&head, 3); // 打印链表 printf("链表内容:"); printList(head); // 找节点 Node *searchResult = searchNode(head, 2); if (searchResult != NULL) { printf("找到节点:%d\n", searchResult->data); } else { printf("未找到节点!\n"); } // 除节点 deleteNode(&head, 2); // 打印链表 printf("除节点后的链表内容:"); printList(head); // 销毁链表 destroyList(head); return 0; } ``` 这段代码实现了链表创建销毁加、除和找功能。您可以根据需要进行修和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

龙跃十二

写的不错,给点鼓励

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值