带头节点单链表操作

简介: #include #include #include using namespace std;typedef long long LL;typedef struct Node{ ...
#include <iostream>
#include <cstdio>
#include <cstdlib>
using namespace std;
typedef long long LL;
typedef struct Node{
    int data;
    struct Node *next;
}*List;
//头插法建立长为n的链表
void FrontCreateList(List &L,int n){
    List p;
    L=(Node*)malloc(sizeof(Node));
    L->next=NULL;
    for(int i=0;i<n;i++){
        p=(Node*)malloc(sizeof(Node));
        cin>>p->data;
        p->next=L->next;
        L->next=p;
    }
}
//尾插法建立长为n的链表
void EndCreateList(List &L,int n){
    List p,q;
    L=(Node*)malloc(sizeof(Node));
    L->next=NULL;
    for(int i=0;i<n;i++){
        p=(Node*)malloc(sizeof(Node));
        cin>>p->data;
        p->next=NULL;
        if(L->next==NULL)
            L->next=p;
        else
            q->next=p;
        q=p;
    }
}
//销毁链表
void Destroy(List &L){
    List p;
    while(L){
        p=L->next;
        free(L);
        L=p;
    }
    cout<<"OK"<<endl;
}
//清空链表
void Clear(List L){
    List p=L->next;
    L->next=NULL;
    Destroy(p);
}
//判断链表是否为空
bool IsEmpty(List L){
    if(L->next==NULL)
        return 1;
    else
        return 0;
}
//返回链表的长度
int Length(List L){
    int j=0;
    List p=L->next;
    while(p){
        j++;
        p=p->next;
    }
    return j;
}
//查找值是否存在
int GetData(List L,int i,int *e){
    int j=0;
    List p=L->next;
    while(p&&j<i){
        j++;
        p=p->next;
    }
    if(!p||i<1)
        return false;
    *e=p->data;
    return true;
}
//得到值的位置
int LocateData(List L,int e){
    int j=0;
    List p=L->next;
    while(p){
        j++;
        if(p->data==e)
            return j;
        p=p->next;
    }
    return false;
}
//在第i的位置插入e
void Insert(List L,int i,int e){
    int j=0;
    List p=L,q,s;
    while(p&&j<i-1){
        j++;
        p=p->next;
    }
    if(!p||i<1)
        exit(0);
    q=p->next;
    s=(Node *)malloc(sizeof(Node));
    if(!s)
        exit(-1);
    s->data=e;
    s->next=q;
    p->next=s;
}
//删除第i位置的节点,并用e返回
void Delete(List L,int i,int *e){
    int j=0;
    List p=L,q;
    while(p&&j<i-1){
        j++;
        p=p->next;
    }
    if(!p||i<1)
        exit(-1);
    q=p->next;
    *e=q->data;
    p->next=q->next;
    free(q);
}
//从头遍历链表
void Travel(List L){
    List p=L->next;
    while(p){
        cout<<p->data<<" ";
        p=p->next;
    }
    cout<<endl;
}
//测试
int main(){
    int n;
    cin>>n;
    List L;
    EndCreateList(L,n);
    Travel(L);
    cout<<Length(L)<<endl<<endl;


    int out,i;
    cin>>i;
    Delete(L,i,&out);
    cout<<out<<endl;
    Travel(L);
    cout<<Length(L)<<endl<<endl;


    int j,put;
    cin>>j>>put;
    Insert(L,j,put);
    Travel(L);
    cout<<Length(L)<<endl<<endl;

    int sear;
    cin>>sear;
    cout<<LocateData(L,sear)<<endl<<endl;

    cout<<IsEmpty(L)<<endl;
    Destroy(L);
    return 0;
}
目录
相关文章
|
8月前
|
存储
带头循环双向链表详解 1
带头循环双向链表详解
|
4月前
|
存储
链表操作详解
链表操作详解
|
4月前
|
存储
数据结构单链表之删除节点 | 第四套
数据结构单链表之删除节点 | 第四套
28 0
|
6月前
|
存储
链表(一) 单链表操作详解
链表(一) 单链表操作详解
30 0
|
8月前
带头循环双向链表详解 2
带头循环双向链表详解
|
10月前
|
存储
|
10月前
|
存储
数据结构之链表(带头双向循环链表)
数据结构之链表(带头双向循环链表)
76 0
|
10月前
|
存储 算法 Java
Java数据结构与算法分析(三)链表(单链表、双链表、环形链表)
通过前篇文章《[数组](https://blog.csdn.net/gozhuyinglong/article/details/109702860)》了解到数组的存储结构是一块连续的内存,插入和删除元素时其每个部分都有可能整体移动。为了避免这样的线性开销,我们需要保证数据可以不连续存储。本篇介绍另一种数据结构:链表。
174 0
|
10月前
|
存储
数据结构——单链表(不带头节点)
链表是一种物理存储结构上非联系,非顺序的存储结构,但数据元素的逻辑顺序是通过链表中的指针链接实现的
88 0