栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > C/C++/C#

C++单链表简单操作

C/C++/C# 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

C++单链表简单操作

#include 
#include 
using namespace std;
typedef struct LNode{
    int data;
    struct LNode* next;
}LNode,*linklist;
// 头插法建立单链表
void HeadInsert(linklist &L){
    int x;
    L = (linklist)malloc(sizeof(LNode)); //申请头节点
    L->next = NULL;
    cout<<"请输入链表每一项,当输入为999时完成链表:";
    cin>>x;
    while (x!=999) {
        LNode *s  = new LNode;
        s->data=x;
        s->next = L->next;
        L->next = s;
        cin>>x;
    }
}
//尾插法建立单链表
void Tailinsert(linklist &L){
    LNode *p;
    int x;
    L = (linklist)malloc(sizeof(LNode));
    p=L;
    cout<<"请输入链表每一项,当输入为999时完成链表:";
    cin>>x;
    while (x!=999) {
        LNode *s =new LNode;
        s->data =x;
        p->next = s;
        p = s;
        cin>>x;
    }
    p->next=NULL;
}
//按序号查找节点值
LNode* GetElem(linklist L,int i){
    int j= 1;
    LNode *p = L->next;
    if (i==0) {
        return L;
    }
    if (i<1) {
        return NULL;
    }
    while (p&&jnext;
        j++;
    }
    return p;
}
//按值查找
LNode* GetElemByint(linklist &L,int x){
    LNode *p=L->next;
    while (p!=NULL&&p->data!=x) {
        p=p->next;
    }
    return p;
}
//插入节点(前插)先查找前驱再插入
void Insertlink(linklist &L,int i,int x){
    LNode *p = GetElem(L, i-1);
    LNode *s = new LNode;
    s->next = p->next;
    p->next = s;
    s->data=x;
}
//删除节点
void DelatePoint(linklist &L,int i){
    LNode *p = GetElem(L, i-1);
    LNode *s =new LNode;
    s=p->next;
    p->next=s->next;
    free(s);
}
int GetLength(linklist L){
    LNode *p = L->next;
    int count = 0;
    while (p!=NULL) {
        p=p->next;
        count++;
    }
    return count;
}
// 打印输出链表
void Printflinklist(linklist L){
    LNode *p = L->next;
    while (p!=NULL) {
        cout<data<<" ";
        p = p->next;
    }
    cout< 

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/589383.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号