关于单链表一些简单的操作(增,删,查,改,排序)

第一次没看书把代码全部敲出来一遍也算一种进步吧。。。

代码是根据自己理解写的,写的很烂可能有些地方处理的不妥当,但大概就是这样吧。

代码很简单,没注释,但很容易看懂。

#include<stdio.h>
#include<stdlib.h>
typedef struct Node
{
    int num;
    int data;
    struct Node *pNext;
}Node,*pNode;
pNode CreateNode(pNode phead,int num,int data)
{
    if (phead == NULL)
    {
        pNode NewNode = (pNode)malloc(sizeof(Node));
        NewNode->num = num;
        NewNode->data = data;
        NewNode->pNext = NULL;
        phead = NewNode;
    }
    else
    {
        pNode p = phead;
        while (p->pNext != NULL)
        {
            p = p->pNext;
        }
        pNode NewNode = (pNode)malloc(sizeof(Node));
        NewNode->num = num;
        NewNode->data = data;
        p->pNext = NewNode;
        NewNode->pNext = NULL;    
    }
    return phead;
}
pNode DeleteNode(pNode phead,int num)
{
    if (phead == NULL)
    {
        printf("链表为空不能删除\n");
        return NULL;
    }
    else if (phead->num == num)
    {
        pNode p = phead;
        phead=p->pNext;
        free(p);
    }
    else
    {
        pNode p1, p2;
        p1 = phead;
        p2 = NULL;
        while (p1->pNext->num != num)
        {
            p1 = p1->pNext;
        }
        p2 = p1->pNext;
        p1->pNext = p2->pNext;
        free(p2);
    }
    return phead;
}
pNode Search(pNode phead, int num)
{
    pNode p = phead;
    while (p != NULL)
    {
        if (p->num == num)
        {            
            return p;
            break;
        }
        p = p->pNext;
    }
    return NULL;
}
pNode AddNodeBack(pNode phead,int num,int inum,int idata)
{
    if (phead == NULL)
    {
        pNode NewNode = (pNode)malloc(sizeof(Node));
        NewNode->num = inum;
        NewNode->data = idata;
        NewNode->pNext = NULL;
        phead = NewNode;
    }
    else
    {
        pNode p = Search(phead, num);
        pNode NewNode = (pNode)malloc(sizeof(Node));
        pNode p1 = NULL;
        NewNode->num = inum;
        NewNode->data = idata;
        p1 = p->pNext;
        p->pNext = NewNode;
        NewNode->pNext = p1;
    }
    return phead;
}
pNode AddNodeAhead(pNode phead, int num, int inum, int idata)
{
    if (phead == NULL)
    {
        pNode NewNode = (pNode)malloc(sizeof(Node));
        NewNode->num = inum;
        NewNode->data = idata;
        NewNode->pNext = NULL;
        phead = NewNode;
    }
    else if (num == 1)
    {    
        pNode p = phead;
        pNode NewNode = (pNode)malloc(sizeof(Node));
        NewNode->num = inum;
        NewNode->data = idata;
        NewNode->pNext = p;
        phead=NewNode;    
    }
    else
    {
        pNode p = Search(phead, num-1);
        pNode NewNode = (pNode)malloc(sizeof(Node));
        pNode p1 = NULL;
        NewNode->num = inum;
        NewNode->data = idata;
        p1 = p->pNext;
        p->pNext = NewNode;
        NewNode->pNext = p1;
    }
    return phead;
}
void SortAllNode(pNode phead, char ch)
{
    if (ch == '>')
    {
        for (pNode p = phead; p != NULL; p = p->pNext)
        {
            for (pNode p1 = phead; p1 != NULL; p1 = p1->pNext)
            {
                if (p->data < p1->data)
                {
                    Node temp;
                    temp.num = p->num;
                    p->num = p1->num;
                    p1->num = temp.num;
                    temp.data = p->data;
                    p->data = p1->data;
                    p1->data = temp.data;
                }
            }
        }

    }
    else if (ch == '<')
    {
        for (pNode p = phead; p != NULL; p = p->pNext)
        {
            for (pNode p1 = phead; p1 != NULL; p1 = p1->pNext)
            {
                if (p->data > p1->data)
                {
                    Node temp;
                    temp.num = p->num;
                    p->num = p1->num;
                    p1->num = temp.num;
                    temp.data = p->data;
                    p->data = p1->data;
                    p1->data = temp.data;
                }
            }
        }
    }
}
void ChangeNode(pNode phead,int num, int NewNum, int NewData)
{
    pNode p = Search(phead, num);
    p->num = NewNum;
    p->data = NewData;
}
void PrintAllNode(pNode phead)
{
    if (phead == NULL)
    {
        printf("链表为空无法显示\n");
        return;
    }
    else
    {
        pNode p = phead;
        while (p!=NULL)
        {
            printf("num=%d data=%d %p %p\n", p->num, p->data,p,p->pNext);
            p = p->pNext;
        }
    }
}
int CountAllNode(pNode phead)
{
    int sum = 0;
    if (phead == NULL)
    {
        printf("链表为空\n");
    }
    else
    {
        pNode p = phead;
        while (p!=NULL)
        {
            p = p->pNext;
            sum++;
        }
    }
    return sum;
}
void *FreeAll(pNode phead)
{
    pNode p1, p2;
    p1 = p2 = NULL;
    p1 = phead;
    while (p1->pNext != NULL)
    {
        p2 = p1->pNext;
        p1->pNext = p2->pNext;
        free(p2);
    }
    free(phead);
    return NULL;
}
int main()
{
    pNode head = NULL;
    head = CreateNode(head, 1, 78);
    head = CreateNode(head, 2, 38);
    head = CreateNode(head, 3, 43);
    head = CreateNode(head, 4, 57);
    head = CreateNode(head, 5, 70);
    head = CreateNode(head, 6, 69);
    head = CreateNode(head, 7, 91);
    PrintAllNode(head);
    printf("总计%d个节点\n", CountAllNode(head));
    head=AddNodeBack(head, 5, 22, 57);
    head = AddNodeAhead(head, 1, 17, 45);
    ChangeNode(head, 7, 100, 100);
    PrintAllNode(head);
    printf("总计%d个节点\n", CountAllNode(head));
    SortAllNode(head, '>');
    PrintAllNode(head);
    head=(pNode)FreeAll(head);
    printf("删除之后\n");
    PrintAllNode(head);
    getchar();
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
当然,我可以为您提供单链表的创建、销毁、加、除和找的代码示例。以下是一个基本的单链表结构的定义和相应的操作函数的实现: ```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; } ``` 这段代码实现了单链表的创建、销毁、加、除和找功能。您可以根据需要进行修和扩展。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值