单链表的一些基本操作(自我总结)

链表可分为两种
1.无头结点定义
2.有头节点定义

无头结点定义的链表:
在这里插入图片描述
它的第一个节点就开始储存数据,这样在进行后续某些操作时会造成一些不方便的地方。
(这是我从啊哈算法中看到的版本,没有分开函数去创立链表)

#include<bits/stdc++.h>
using namespace std;    
//链表是由多个结构体组成,其中的指针指向的下一个结构体的地址
struct node{
    int data;
    struct node *next;
};

int main(){
    struct node *head,*p,*q,*t;
    int i,n,a;
    scanf("%d",&n);
    head=NULL;
    for(i=1;i<=n;i++){
        scanf("%d",&a);
        p=(struct node*)malloc(sizeof(struct node));
         // p是创建节点的工具人,申请的一个内存
        p->data=a;
 // p节点分为两部分,第一个是 p->data 用来储存数据(data是多个结构体中的 int data 储存的整型变量)
        p->next=NULL; // 第二部分是 p->next 指向下一节点的指针
        if(head==NULL)
            head=p;
        else
            q->next=p; // q也是工具人,它储存了上一个节点p的地址
        q=p; // 让q指向p
    }
    scanf("%d",&a); //要插入的数
    t=head;
    while(t!=NULL){
        if(t->next==NULL || t->next->data > a){
            p=(struct node*)malloc(sizeof(struct node));
            p->data=a;
            p->next=t->next;//先让p指向下一节点
            t->next=p;//再让当前节点指向p
            break;
        }
        t=t->next;
    }
    t=head;
    while(t!=NULL){
        printf("%d ",t->data);
        t=t->next;
    }
    printf("\n");
    getchar();getchar();
    return 0;
}

有头节点链表定义:在这里插入图片描述

#include<bits/stdc++.h>
using namespace std;
struct node{
    int data;
    struct node *next;
};

struct node* creat(int n){ // 链表的录入
    struct node *head,*L,*Lnew;
    head=(node*)malloc(sizeof(node));
    head->next=NULL;
    L=head;
    for(int i=0;i<n;i++){
    	Lnew=(node*)malloc(sizeof(node);
    	cin >> Lnew->data;
        Lnew->next=NULL;
        L->next=Lnew;
        L=Lnew;
    }
    return head;
}
void show(struct node *head){ //链表的输出
    struct node *p;
    p=head->next;
    while(p!=NULL){
        cout << p->data << ' ' ;
        p=p->next;
    }
    cout << endl;
}
void deleteNode(struct node *head,int m){ // 链表节点的删除
    struct node *L,*Lnew,*temp;
    L=head;
    Lnew=head->next;
    while(Lnew!=NULL){
        if(Lnew->data==m){
            temp=Lnew;
            Lnew=Lnew->next;
            L->next=Lnew;
            free(temp);
        }
        else{
            L=Lnew;
            Lnew=Lnew->next;
        }
    }
}
void addNode(struct node *head,int a){ //链表节点的有序增加
    struct node *L,*Lnew,*p;
    p=(node*)malloc(sizeof(node));
    p->data=a;
    p->next=NULL;
    L=head;
    Lnew=head->next;
    while(Lnew!=NULL){
        if(Lnew->data > a && L->next!=NULL){
            L->next=p;
            p->next=Lnew;
            break;
        }
        else if(Lnew->next==NULL){
            Lnew->next=p;
            break;
        }
        Lnew=Lnew->next;
        L=L->next;
    }
}
void turnDownNode(node* head){ //链表的倒置
    node *L,*Lnew;
    L=head->next;
    head->next=NULL;
    while(L){
        Lnew=L->next;
        L->next=head->next;
        head->next=L;
        L=Lnew;
    }
}
int main(){
    int n,m;
    cout << "输入链表节点数:";
    cin >> n;
    struct node*head;
    head=creat(n);
    cout << "输入要删除的数据:";
    cin >> m;
    cout << endl;
    cout << "-------------------------" << endl;
    deleteNode(head,m);
    show(head);
    int x;
    cout << "输入要插入的数据:";
    cin >> x;
    addNode(head,x);
    show(head);
    return 0;
}
发布了5 篇原创文章 · 获赞 0 · 访问量 143

猜你喜欢

转载自blog.csdn.net/weixin_45928836/article/details/103996125