线性表的链式表示——单链表;头插,尾插,按值查找,按序号查找,插入,删除;

cpp 复制代码
#include <iostream>
#include <algorithm>//fill()
#define InitSize 5

using namespace std;

/*线性表:链式表示------单链表;头插,尾插,按值查找,按序号查找,插入,删除*/
typedef struct LNode{
    int data;
    struct LNode *next;
}LNode,*LinkList;

void List_Output(LinkList L){
    LNode *s;
    s=L->next;
    while(s!=NULL){
        printf("%d ",s->data);
        s=s->next;
    }
    printf("\n");
}

LinkList List_HeadInsert(LinkList &L){
    LNode *s;
    int x;
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
    scanf("%d",&x);
    while(x!=999){
        s=(LNode *)malloc(sizeof(LNode));
        s->data=x;
        s->next=L->next;
        L->next=s;
        scanf("%d",&x);
    }
    return L;
}

LinkList List_TailInsert(LinkList &L){
    LNode *s,*r;
    int x;
    L=(LNode *)malloc(sizeof(LNode));
    L->next=NULL;
    r=L;
    scanf("%d",&x);
    while(x!=999){
        s=(LNode *)malloc(sizeof(LNode));
        s->data=x;
        r->next=s;
        r=s;
        scanf("%d",&x);
    }
    r->next=NULL;
    return L;
}

LNode *GetElem(LinkList L,int i){//得到第i个位置(头节点的下一个节点为第1个位置);
    LNode *p=L->next;
    int j=1;
    if(i==0)
        return NULL;
    while(p&&j<i){
        p=p->next;
        j++;
    }
    return p;
}

LNode *GetElem2(LinkList L,int e){
    LNode *p=L->next;
    while(p!=NULL&&p->data!=e)
        p=p->next;
    return p;
}

LinkList List_Insert(LinkList &L,LNode *s,int i){//插入到第i个位置(头节点的下一个节点为第1个位置);前插操作:先找到i-1,插到i-1的后面
    LNode *p;
    p=GetElem(L,i-1);
    s->next=p->next;
    p->next=s;
    return L;
}

LinkList List_Insert2(LinkList &L,LNode *s,LNode *p){//已给出s和p,将s插入到p的前面:先插到后面,再交换值
    s->next=p->next;
    p->next=s;
    int temp;
    temp=p->data;
    p->data=s->data;
    s->data=temp;
    return L;
}

LinkList List_Delete(LinkList &L,int i){//删除第i个结点
    LNode *p;
    p=GetElem(L,i-1);
    LNode *q;
    q=(LNode *)malloc(sizeof(LNode));
    q=p->next;
    p->next=q->next;
    free(q);
    return L;
}

LinkList List_Delete2(LinkList &L,LNode *p){
    LNode *q;
    q=(LNode *)malloc(sizeof(LNode));
    q=p->next;
    p->data=p->next->data;
    p->next=q->next;
    free(q);
    return L;
}
int main()
{

    /*线性表:链式表示------单链表;头插,尾插,按值查找,按序号查找,插入第i个位置,插入已给结点的后面,删除第i个结点,删除已给结点*/
    /*LinkList L2;
    List_HeadInsert(L2);
    List_Output(L2);*/

    LinkList L;
    List_TailInsert(L);
    List_Output(L);

    //LNode *s;
    //s=GetElem(L,2)//获取第i个结点(头节点的下一个节点为第1个位置)
    //printf("%d\n",s->data);

    //LNode *s1=GetElem(L,3);
    //GetElem2(L,2);
    //printf("%d\n",s1->data);

    /*LNode *s3;
    s3=(LNode *)malloc(sizeof(LNode));
    s3->data=9;
    List_Insert(L,s3,2);//插入到第i个位置
    List_Output(L);*/

    /*LNode *p=GetElem(L,2);
    LNode *s2;
    s2=(LNode *)malloc(sizeof(LNode));
    s2->data=9;
    List_Insert2(L,s2,p);//将s插入到p的前面
    List_Output(L);*/

    //List_Delete(L,2);
    //List_Output(L);

    /*LNode *p;
    p=L->next->next;//给出需要删除的结点
    List_Delete2(L,p);
    List_Output(L);*/

    return 0;
}
相关推荐
老四啊laosi2 小时前
[C++进阶] 24. 哈希表封装unordered_map && unordered_set
c++·哈希表·封装·unordered_map·unordered_set
妙为3 小时前
银河麒麟V4下编译Qt5.12.12源码
c++·qt·国产化·osg3.6.5·osgearth3.2·银河麒麟v4
史迪仔01126 小时前
[QML] QML IMage图像处理
开发语言·前端·javascript·c++·qt
会编程的土豆7 小时前
【数据结构与算法】再次全面了解LCS底层
开发语言·数据结构·c++·算法
低频电磁之道7 小时前
解决 Windows C++ DLL 导出类不可见的编译错误
c++·windows
君义_noip9 小时前
信息学奥赛一本通 4150:【GESP2509七级】⾦币收集 | 洛谷 P14078 [GESP202509 七级] 金币收集
c++·算法·gesp·信息学奥赛·csp-s
Ricky_Theseus9 小时前
静态链接与动态链接
c++
澈2079 小时前
双指针,数组去重
c++·算法
小辉同志10 小时前
207. 课程表
c++·算法·力扣·图论
feng_you_ying_li10 小时前
C++11,{}的初始化情况与左右值及其引用
开发语言·数据结构·c++