单链表各个功能的实现

本文详细介绍了使用C++实现一个简单的单向链表,包括节点的创建、在链表尾部和头部插入、删除节点、遍历链表以及销毁链表等基本操作。
摘要由CSDN通过智能技术生成

#include<bits/stdc++.h>
#include<iostream>
#include<stdlib.h>
#include<assert.h>
using namespace std;
typedef struct stlist {
    int data;
    stlist* next;
} stlnode;
stlnode* buynode(int x){//创造结点
    stlnode *pos=(stlnode*)malloc(sizeof(stlnode*));
    pos->data=x;
    pos->next=NULL;
    return pos;
}
void printlist(stlnode* phead) {
    stlnode* cur=phead;
    while(cur) {
        cout<<cur->data;
        cur=cur->next;
    }

}
void pushback(stlnode** phead,int x) { //尾插
    assert(phead);
    stlnode* newnode=(stlnode*) malloc(sizeof(stlnode));
    newnode->data=x;
    newnode->next=NULL;
    if(*phead==NULL) *phead=newnode;
    else {
        stlnode* tail=*phead;
        while(tail->next) {
            tail=tail->next;
        }
        tail->next=newnode;
    }
}
void deletestlplist(stlnode **phead) { //尾删
    assert(phead);
    assert(*phead);
    if((*phead)->next==NULL) {
        delete(*phead);
        *phead=NULL;
    } else {
        stlnode * tail=*phead;
        while(tail->next->next) {
            tail=tail->next;
        }
        delete(tail->next);
        tail->next=NULL;
    }
}

void headpush(stlnode** phead,int x) { //头插
    assert(phead);
    stlnode * newnode=(stlnode*)malloc(sizeof(stlnode));
    newnode->data=x;
    newnode->next=*phead;
    *phead=newnode;
}
void insertnode(stlnode** phead,stlnode*pos,int x){//任意位置插入
    stlnode*newnode=(stlnode*)malloc(sizeof(stlnode*));
    if(*phead==pos){
        newnode->next=*phead;
        *phead=newnode;
    }
    else{
        stlnode*prev=*phead;
        while(prev->next!=pos){
            prev=prev->next;
        }
        prev->next=newnode;
        newnode->next=pos;
    }
}


void headdelete(stlnode** phead) { //头删
    assert(phead);
    assert(*phead);
    stlnode* cur=*phead;
    *phead=(*phead)->next;
    delete(cur);
    cur=NULL;
}

void deletenode(stlnode** phead,stlnode* tmp) { //删除给定的结点
    assert(phead);
    assert(tmp);
    if(*phead==tmp) {
        stlnode * cur=*phead;
        *phead=(*phead)->next;
        delete(cur);
        cur==NULL;
    } else {
        stlnode * p=*phead;
        while(p->next!=tmp) {
            p=p->next;
            assert(p->next);
        }
        p->next=tmp->next;
        delete(tmp);
        tmp=NULL;
    }
}
stlnode* findnode(stlnode* phead,int x) { //寻找结点
    stlnode*cur=phead;
    while(cur) {
        if(cur->data==x) {
            return cur;
        }
        cur=cur->next;
    }
    return NULL;
}
void changenode(stlnode** phead,int y,int x) { //更改某个结点
    stlnode*cur=*phead;
    for(int i=1; i<y; i++) {
        cur=cur->next;
    }
    cur->data=x;
}
void listdestory(stlnode** phead){//链表销毁
    assert(phead);
    stlnode* cur=*phead;
    stlnode* pos;
    while(cur){
        pos=cur->next;
        delete(cur);
        cur=pos;
    }
    *phead=NULL;
}
int main() {
    stlnode* plist=NULL;
    pushback(&plist,1);
    pushback(&plist,2);
    pushback(&plist,3);
    pushback(&plist,4);
    printlist(plist);
    cout<<endl;
    deletestlplist(&plist);
    printlist(plist);
    cout<<endl;
    headdelete(&plist);
    printlist(plist);
    cout<<endl;
    headpush(&plist,8);
    printlist(plist);
    cout<<endl;
    changenode(&plist,2,6);
    printlist(plist);
    cout<<endl;
    stlnode* pos=findnode(plist,6);
    int i=1;
    while(pos){
        cout<<++i<<endl<<pos;
        pos=findnode(pos->next,2);
    }
}

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值