单链表的创建及对应操作(增删改查)

#include <iostream>
using namespace std;

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

//初始化链表
node* initList(){
    node* head = (node*)malloc(sizeof(node));
    node* temp = head;//临时节点记录头结点,不能直接使用头结点遍历,否则容易造成头结点地址丢失
    for(int i=0;i<5;i++)
    {
        node* no = (node*)malloc(sizeof(node));
        no->data = i;
        no->next = NULL;//必须将新节点的next指向NULL,否则遍历时无法判断尾节点是否为空
        temp->next=no;
        temp = temp->next;
    }
    return head;
}

//打印链表中的数据
void show(node* head)
{
    node* temp = head;
    while(temp->next!=NULL)
    {
        temp = temp->next;
        cout<<temp->data<<endl;
    }
}

//向表尾添加一个节点
void addNode(node* head,int elem)
{
    if(head==NULL)
    {
        cout<<"表空"<<endl;
        return;
    }
    cout<<"向表尾添加元素"<<endl;
    node* temp = head;
    while(temp->next!=NULL)
    {
        temp = temp->next;
    }
    node* no = (node*)malloc(sizeof(node));
    no->data = elem;
    no->next = NULL;
    temp->next = no;
}

//添加指定位置节点
void addNode(node* head,int pos,int elem)
{
    if(head==NULL)
    {
        cout<<"表空"<<endl;
        return;
    }

    cout<<"向指定位置添加元素"<<endl;
    node* temp = head;
    for(int i=0;i<pos;i++)
    {
        temp = temp->next;
    }
    node* no = (node*)malloc(sizeof(node));
    no->data = elem;
    no->next = temp->next;//必须先让新节点的next指针,指向待插入节点位置的下一个节点,直接让temp的next指向新节点,使temp后续节点丢失
    temp->next = no;//让待插入节点的上一个节点next指向新节点
}

//删除指定位置元素,返回被删除的元素
int removeNodeByIndex(node* head,int pos)
{
    if(head==NULL)
    {
        cout<<"表空"<<endl;
        return -1;
    }
    cout<<"删除指定位置元素"<<endl;
    int elem;
    node* temp = head;
    for(int i=0;i<pos;i++)
    {
        temp = temp->next;
    }
    node* p = temp->next;//用一个临时变量记录删除接节点地址
    elem = p->data;
    temp->next = temp->next->next;//使待删除节点的上一个节点的指针,指向待删除节点的下一个节点
    free(p);
    return elem;
}

//根据给定值删除节点
void removeNodeByElem(node* head,int elem)
{
    if(head==NULL)
    {
        cout<<"表空"<<endl;
        return;
    }
    cout<<"删除给定元素"<<endl;
    node* temp = head;//用于遍历链表
    node* temp2 = NULL;//用于temp指向的上一个节点
    while(temp->next!=NULL) {
        temp2 = temp;
        temp = temp->next;
        if (temp->data == elem)
            break;
    }
    node* p = temp2->next;//用一个临时变量记录删除接节点地址
    temp2->next = temp2->next->next;//使待删除节点的上一个节点的指针,指向待删除节点的下一个节点
    free(p);
}

//修改节点值
void replaceNodeByIndex(node* head,int pos,int elem)
{
    if(head==NULL)
    {
        cout<<"表空"<<endl;
        return;
    }
    node* temp = head;
    for(int i=0;i<pos;i++)
    {
        temp = temp->next;
    }
    temp->data = elem;
}

//查询链表中是否存在给定值
int selectElem(node* head,int elem)
{
    int status = -1;
    node* temp = head;
    while(temp->next!=NULL)
    {
        temp=temp->next;
        if(temp->data == elem)
            status = 1;
    }
    return status;
}


int main()
{
    node* head = initList();
    show(head);


    addNode(head,6);
    cout<<"-------------------"<<endl;
    show(head);


    cout<<"-------------------"<<endl;
    addNode(head,2,9);
    show(head);


    cout<<"-------------------"<<endl;
    int elem = removeNodeByIndex(head,2);
    show(head);


    cout<<"-------------------"<<endl;
    removeNodeByElem(head,2);
    show(head);


    cout<<"-------------------"<<endl;
    replaceNodeByIndex(head,2,12);
    show(head);

    cout<<"-------------------"<<endl;
    int status = selectElem(head,4);
    cout<<"status:"<<status<<endl;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值