数据结构 -------单链表

//单链表的初始化



#include <stdio.h>
#include <stdlib.h>
//链表中节点的结构
typedef struct Link{
    int elem;   //数据域
    struct  Link *next;  //指针域
}link;

//初始化链表的函数
link *initLink()
{
    link *p=NULL; //创建头指针
    link *temp=(link*)malloc(sizeof(link));//创建首元节点
    //首元节点先初始化
    temp->elem=1;
    temp->next=NULL;
    p=temp;
    for(int i=2;i<5;i++)
    {
        link *a=(link*)malloc(sizeof(link));
        a->elem=i;
        a->next=NULL;
        temp->next=a;
        temp=temp->next; //temp=a;
    }
    return p;
}

void display(link *p)
{
    link* temp=p;//将 temp 指针重新指向头结点
    //只要 temp 指针指向的结点的 next 不是 Null,就执行输出语句。
    while(temp){
        printf("%d",temp->elem);
        temp=temp->next;
    }
    printf("\n");
}

int main()
{
    //初始化链表(1,2,3,4)
    printf("初始化链表为:\n");
    link *p=initLink();
    display(p);
    return 0;
}









运行结果:
root@book-virtual-machine:/mnt/hgfs/lua/C++# gcc  salman_0119.cpp -o salman_0119
root@book-virtual-machine:/mnt/hgfs/lua/C++# ./salman_0119
初始化链表为:
1234
root@book-virtual-machine:/mnt/hgfs/lua/C++#

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

typedef struct Link{
    int elem;
    struct Link *next;
}link;

link * initLink();
//链表插入的函数,p 是链表,elem 是插入的结点的数据域,add 是插入的位置
link * insertElem(link * p, int elem, int add);
//删除结点的函数,p 代表操作链表,add 代表删除节点的位置
link * delElem(link * p, int add);
//查找结点的函数,elem 为目标结点的数据域的值
int selectElem(link * p, int elem);
//更新结点的函数,newElem 为新的数据域的值
link *amendElem(link * p, int add, int newElem);
void display(link *p);


int main()
{
    //初始化链表(1,2,3,4)
    printf("初始化链表为:\n");
    link *p = initLink();
    display(p);

    printf("在第 4 的位置插入元素 5:\n");
    p = insertElem(p, 5, 4);
    display(p);


    printf("删除元素 3:\n");
    p = delElem(p, 3);
    display(p);

    printf("查找元素 2 的位置为:\n");
    int address = selectElem(p, 2);
    if (address == -1) {
        printf("没有该元素");
    }
    else {
        printf("元素 2 的位置为:%d\n", address);
        }

        printf("更改第 5 的位置上的数据为 7:\n");
        p = amendElem(p, 5, 7);
        display(p);
        return 0;
}



link * initLink(){
    link *p=(link*)malloc(sizeof(link));   //创建一个头结点
    link *temp=p;                           //声明一个指针指向头结点,用于遍历链表

    //生成链表
    for(int i=1;i<5;i++)
    {
        link *a=(link*)malloc(sizeof(link));
        a->elem=i;
        a->next=NULL;
        temp->next=a;
        temp=temp->next;  //temp=a;
    }
    return p;
}
//链表插入的函数,p 是链表,elem 是插入的结点的数据域,add 是插入的位置
link * insertElem(link * p, int elem, int add){
    link* temp=p;   //创建临时结点 temp
    //首先找到要插入位置的上一个结点
    for(int i=1;i<add;i++)
    {
        temp=temp->next;
        if(temp==NULL)
        {
            printf("插入位置无效\n");
            return p;
        }
    }
    //创建插入结点 c
    link *c=(link*)malloc(sizeof(link));
    c->elem=elem;
    c->next=temp->next;
    temp->next=c;
    return p;
}

link *delElem(link *p,int add){
    link *temp=p;
    //遍历到被删除结点的上一个结点
    for(int i=1;i<add;i++)
    {
        temp = temp->next;
        if(temp->next==NULL)
        {
            printf("没有该结点\n");
            return p;
        }
    }

    link *del=temp->next;//单独设置一个指针指向被删除结点,以防丢失
    temp->next=temp->next->next;//删除某个结点的方法就是更改前一个结点的
    free(del);//手动释放该结点,防止内存泄漏
    return p;
}


int selectElem(link *p,int elem)
{
    link*t= p;
    int i=1;
    while(t->next)
    {
        t=t->next;
        if(t->elem==elem)
        {
            return i;
        }
        i++;
    }
    return -1;
}


link *amendElem(link *p,int add,int newElem)
{
    link *temp=p;
    temp=temp->next;   temp 指向首元结点
    //temp 指向被删除结点
    for(int i=1;i<add;i++)
    {
       
        temp=temp->next;

         if(temp==NULL)
        {  
            printf("没有该结点\n");
            return p;
        }
         
    }
    temp->elem=newElem;
    return p;

}

void display(link *p) {
    link *temp=p;   //将 temp 指针重新指向头结点
    //只要 temp 指针指向的结点的 next 不是 Null,就执行输出语句。
    while(temp->next)
    {
        temp=temp->next;
        printf("%d ", temp->elem);
    }
    printf("\n");
}




运行结果:
root@book-virtual-machine:/mnt/hgfs/lua/C++# gcc  salman_0119.cpp -o salman_0119
root@book-virtual-machine:/mnt/hgfs/lua/C++# ./salman_0119
初始化链表为:
1 2 3 4
在第 4 的位置插入元素 5:
1 2 3 5 4
删除元素 3:
1 2 5 4
查找元素 2 的位置为:
元素 2 的位置为:2
更改第 5 的位置上的数据为 7:
没有该结点
1 2 5 4
root@book-virtual-machine:/mnt/hgfs/lua/C++#

猜你喜欢

转载自blog.csdn.net/aa804738534/article/details/113594515