【15】链表基本操作(c语言代码)

单链表

增删改查操作

#include <stdlib.h>
#include <stdio.h>
typedef struct Node { int data; struct Node * next; }Node; // 打印链表 void PrintList(Node * head) { Node * cur = head; while(cur->next) { printf(" %d ", cur->data); cur = cur->next; } printf("\n"); } // 创建链表 Node * CreateList() { Node * head = (Node*) malloc (sizeof(Node)); if (NULL == head) exit(-1); head->next = NULL; return head; } // void InsertList(Node * head, int data) { Node * cur = (Node*) malloc (sizeof(Node)); cur->data = data; cur->next = head->next; head->next = cur; } // void InsertListByPos(Node * head, int pos, int data) { // 找到待插入位置 Node * temp = head; int i; for(i=0; i<pos; i++) { temp = temp->next; } // 插入新的元素 Node * cur = (Node *) malloc (sizeof(Node)); cur->data = data; cur->next = temp->next; temp->next = cur; } // void DeleteListByPos(Node * head, int pos) { Node * cur = head; int i; for(i=0; i<pos; i++) { cur = cur->next; } Node * del = cur->next; // 单独设置一个被删除的结点 cur->next = cur->next->next; free(del); return; } void ChangeList(Node * head, int pos, int data) { Node * cur = head; // cur = cur->next; 放前放后都可以,先指向首元结点 int i = 0; for(i=0; i<pos; i++) { cur = cur->next; } cur = cur->next; cur->data = data; } int FindList(Node * head, int data) { Node * cur = head; int n = 0; while(cur->next) { if (cur->data == data) { return n; } cur = cur->next; n++; } } void main() { printf("创建链表:\n"); Node * head = CreateList(); PrintList(head); printf("插入链表:\n"); int i = 0; for(i=0; i<100; i+=10) { InsertList(head, i); } PrintList(head); printf("根据位置插入链表:\n"); InsertListByPos(head, 2, 111); InsertListByPos(head, 2, 666); InsertListByPos(head, 4, 888); PrintList(head); printf("删除链表:\n"); DeleteListByPos(head, 2); PrintList(head); printf("更改链表:\n"); ChangeList(head, 2, 6661); PrintList(head); printf("查找链表:\n"); int find = FindList(head, 20); printf("find = %d\n", find); }

 

其它操作

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

typedef struct Node
{
        int data;
        struct Node * next;
}Node;

// 打印链表
void PrintList(Node * head)
{
        Node * cur = head;
        while(cur->next) {
                printf(" %d ", cur->data);
                cur = cur->next;
        }
        printf("\n");
}

// 创建链表
Node * CreateList()
{
        Node * head = (Node*) malloc (sizeof(Node));
        if (NULL == head) exit(-1);
        head->next = NULL;
        return head;
}

//
void InsertList(Node * head, int data)
{
        Node * cur = (Node*) malloc (sizeof(Node));
        cur->data = data;

        cur->next = head->next;
        head->next = cur;
}

Node * ReverseList(Node * head)
{
        if (NULL == head || NULL == head->next) {
                return head;
        }

        Node * cur = head;
        Node * new = NULL; // 新链表
        while(cur != NULL) {
                Node * temp = cur->next;
                cur->next = new;
                new = cur;
                cur = temp;
        }
        return new;
}

/*
思想:
        判断是否有环
        用两个指针,指针A一次向前走一步,指针B一次向前走两步
        如果A和B能够相遇,说明链表中有环
*/
int HasCycle(Node * head)
{
        if (NULL == head) {
                return 0;
        }

        Node * fast = head;
        Node * slow = head;

        // fast->next可能为空
        while(slow && fast && fast->next) {
                slow = slow->next;
                fast = fast->next->next;
                if (slow == fast) {
                        return 1;
                }
        }
        return 0;
}

/*
思想:
        找出倒数第k个结点
        两个指针均指向头结点
        让B先走K步
        A和B一起往前走,知道B走到NULL为止
*/
void main()
{
        printf("创建链表:\n");
        Node * head = CreateList();
        PrintList(head);

        printf("插入链表:\n");
        int i;
        for(i=0; i<100; i+=10) {
                InsertList(head, i);
        }
        PrintList(head);

        printf("逆置链表:\n");
        Node * new = ReverseList(head);
        PrintList(new);

        int has = HasCycle(head);
        printf("是否为环链表: %d\n", has);
}

 

 

 

双链表

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

// 双向链表
typedef struct DoubleNode
{
        int data;
        struct DoubleNode * pre, * next;
}Node;

void PrintList(Node * head)
{
        Node * cur = head->next;
        while(cur != head) {
                printf(" %d  ", cur->data);
                cur = cur->next;
        }
        printf("\n");
}

int LenList(Node * head)
{
        Node * cur = head->next;
        int n;
        while(cur != head) {
                cur = cur->next;
                n++;
        }
        return n;
}

// 创建链表
Node * CreateList()
{
        Node * head = (Node*) malloc (sizeof(Node));
        if (NULL == head) exit(-1);
        head->next = head;
        head->pre = head;
        return head;
}

//
void InsertList(Node * head, int data)
{
        Node * cur = (Node*) malloc (sizeof(Node));
        if (NULL == cur) exit(-1);

        cur->data = data;

        cur->next = head->next;
        head->next = cur;

        // cur->pre = head; 可以
        // cur->pre->next = cur; 不可以
        cur->pre = head->pre;
        cur->next->pre = cur;
}

//
void DeleteList1(Node * head, int data)
{
        Node * cur = head->next;
        while(cur != head) {
                if (cur->data == data) {
                        cur->pre->next = cur->next;
                        cur->next->pre = cur->pre;
                        free(cur);
                }
                cur = cur->next;
        }
}

// 销毁
void DestoryList(Node * head)
{
        head->pre->next = NULL;
        Node * cur;
        while(head) {
                cur = head;
                head = head->next;
                free(cur);
        }
}

int main()
{
        printf("创建链表:\n");
        Node * head = CreateList();
        PrintList(head);

        printf("插入链表:\n");
        int i;
        for(i=0; i<100; i+=10) {
                InsertList(head, i);
        }
        PrintList(head);

        printf("删除链表:\n");
        DeleteList(head, 40);
        PrintList(head);

        DestoryList(head);

 

posted @ 2020-09-09 10:32  欧阳图图的少年成长记  阅读(499)  评论(0编辑  收藏  举报