带头节点单链表操作

简介: #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;
}
目录
相关文章
|
3月前
最复杂的链表(带哨兵位的双向循环链表)
最复杂的链表(带哨兵位的双向循环链表)
|
4月前
特殊链表(循环单链表,循环双链表,静态链表)
特殊链表(循环单链表,循环双链表,静态链表)
38 3
|
4月前
|
存储 算法 C语言
链表带头和不带头的区别及其应用
链表带头和不带头的区别及其应用
带头循环双向链表详解 1
带头循环双向链表详解
|
11月前
|
存储
链表(一) 单链表操作详解
链表(一) 单链表操作详解
45 0
无头单向不循环链表和带头双向循环链表的创建
接下来我们将会了解最基础的链表--->单链表 以及最方便也是最爽的链表--->带头双向循环链表。
39 0
链表oj题讲解(2)之链表中间节点
首先拿到题目,小编的第一个思路是先求得链表的长度,然后将寻求长度的2分之一加1处,这里我们就需要我们用单个指针实现了。
61 1