链表

静态链表

#include <stdio.h>
#include <stdlib.h>
struct SingleList
{
    //数据域
    int data;        //研究对象
    //指针域
    struct SingleList *next;
};

int main()
{
    //三个结构体变量
    struct SingleList  Node1 = { 1, NULL };
    struct SingleList  Node2 = { 2, NULL };
    struct SingleList  Node3 = { 3, NULL };
    struct SingleList* p1 = &Node1;
    struct SingleList* p2 = &Node2;
    struct SingleList* p3 = &Node3;
    //p1->next = p2;
    //p2->next = p3;
    (&Node1)->next = &Node2;
    //Node1.next = &Node2;
    Node2.next = &Node3;
    //链表打印
    struct SingleList* p = &Node1;
    while (p)
    {
        printf("%d->", p->data);
        p = p->next;
    }
    return 0;

}
View Code

 

动态链表

 

#include <stdio.h>
#include <stdlib.h>
/*
    1.创建过程
    2.操作:增删改查
    3.遍历:打印
*/
//                创建数据结构体
//***********************************
struct SingleList
{
    int data;
    struct SingleList * next;
};

//                  创建表头:有表头的链表
//******************************************
struct SingleList *createListHead()
{
    //指针:1.要初始化
        //1.普通变量的地址   2.堆内存
    struct SingleList* headNode = (struct SingleList *)malloc(sizeof(struct SingleList));
    //用指针表示结构体变量 
    //headNode->data  表头的差异化处理,不存放数据
    headNode->next = NULL;
    return headNode;
}
//                创建结点: 
//*******************************************
struct SingleList * createNode(int data)
{
    struct SingleList *newNode = (struct SingleList *)malloc(sizeof(struct SingleList));
    newNode->data = data;
    //(*newNode).data = data;
    newNode->next = NULL;
    //(*newNode).next = next;
    return newNode;
}
//                链表的插入:表头插入
void insertNodeByHead(struct SingleList *headNode, int data)
{
    //插入结点:创建结点---调用创建结点的函数
    struct SingleList *newNode = createNode(data);
    //插入:画图
    //想象一下插队,是不是要插在原来人的前面
    newNode->next = headNode->next;
    headNode->next = newNode;

}
//                链表的插入:表尾插入
//*************************************************
void  inserNodeByBack(struct SingleList* headNode, int data)
{
    //创建插入结点
    struct SingleList*  newNode = createNode(data);
    //找表尾
    struct SingleList* p = headNode;
    //当下一个结点不为NULL 一一直往下走
    while (p->next!=NULL)
    {
        p = p->next;
    }
    p->next = newNode;
}
//                指定位置插入
//************************************************
//插入那个链表,插入的数据是多少,插入的位置数据去衡量
void inserNodeByAppoin(struct SingleList * headNode, int data, int posData)
{
    //1.找到插入的位置 :两个相邻的指针
    struct SingleList* p = headNode;
    struct SingleList* q = headNode->next;
    //表头后面没有结点,链表为空
    if (q == NULL)
    {
        printf("链表为空,无法插入");
        return;
    }
    else
    {
        while (q->data != posData)
        {
            //q=q->next ,p=p->next
            p = q;    //前面那个指针走到后面那个指针的位置
            q = p->next;    //后面那个指针的位置走到原来的下一个
            if (q == NULL)
            {
                printf("未找到指定位置,无法插入");
                return;
            }
        }
        struct SingleList* newNode = createNode(data);
        p->next = newNode;
        newNode->next = q;
    }
}



//                指定位置删除
void deleteNodeByAppoin(struct SingleList *headNode, int posData)
{
    struct SingleList* p = headNode;
    struct SingleList* q = headNode->next;
    //表头后面没有结点,链表为空
    if (q == NULL)
    {
        printf("链表为空,无法删除");
        return;
    }
    else
    {
        while (q->data != posData)
        {
            //q=q->next ,p=p->next
            p = q;    //前面那个指针走到后面那个指针的位置
            q = p->next;    //后面那个指针的位置走到原来的下一个
            if (q == NULL)
            {
                printf("未找到指定位置,无法删除");
                return;
            }
        }
        p->next = q->next;
        free(q);
    }
}

//                查找
//*************************************************
struct SingleList * searchNode(struct SingleList* headNode, int posData)
{
    struct SingleList* p = headNode;
    if (p->next == NULL)
    {
        return NULL;
    }
    else
    {
        while (p->data != posData)
        {
            p = p->next;
            if (p == NULL)
            {
                return NULL;
            }
        }
        return p;
    }
}
//作业
//表尾删除+表头删除

//                链表的打印
//************************************************
void printList(struct SingleList *list)
{
    //表头未存储数据,所以从第二结点开始打印
    struct SingleList* p = list->next;
    while (p)
    {
        printf("%d->", p->data);
        p = p->next;
    }
    printf("\n");
}
int main()
{
    //1.创建链表
    struct  SingleList* list = createListHead();
    //指针申请堆内存
    insertNodeByHead(list, 1);
    insertNodeByHead(list, 2);
    insertNodeByHead(list, 3);
    printList(list);
    inserNodeByBack(list, 0);
    inserNodeByBack(list, 4);
    printList(list);
    inserNodeByAppoin(list, 5, 4);
    printList(list);
    system("pause");
    return 0;
}
View Code

 

posted @ 2020-08-31 16:55  KD-VS-WB  阅读(75)  评论(0)    收藏  举报