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

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;
}
相关推荐
澈2074 小时前
深入浅出C++滑动窗口算法:原理、实现与实战应用详解
数据结构·c++·算法
A.A呐4 小时前
【C++第二十九章】IO流
开发语言·c++
ambition202424 小时前
从暴力搜索到理论最优:一道任务调度问题的完整算法演进历程
c语言·数据结构·c++·算法·贪心算法·深度优先
kebeiovo4 小时前
atomic原子操作实现无锁队列
服务器·c++
Yungoal4 小时前
常见 时间复杂度计算
c++·算法
6Hzlia5 小时前
【Hot 100 刷题计划】 LeetCode 48. 旋转图像 | C++ 矩阵变换题解
c++·leetcode·矩阵
Ricky_Theseus6 小时前
C++右值引用
java·开发语言·c++
吴梓穆6 小时前
UE5 c++ 常用方法
java·c++·ue5
云栖梦泽6 小时前
Linux内核与驱动:9.Linux 驱动 API 封装
linux·c++
Morwit6 小时前
【力扣hot100】 1. 两数之和
数据结构·c++·算法·leetcode·职场和发展