单链表(插入、查找、删除、打印)[c++&c]——【强烈推荐】深入浅出数据结构 - 顶尖程序员图文讲解 - UP主翻译校对 (已完结)

学习的是↓
【强烈推荐】深入浅出数据结构 - 顶尖程序员图文讲解 - UP主翻译校对 (已完结)


以下为个人学习笔记(视频P5-P8)

单链表(插入、查找、删除、打印)[c++&c]

1、向链表中插入一个节点
尾插法

void Insert(int x)//在链表尾部插入一个值为x的节点
{
	Node*temp=new Node();
	temp->data=x;
	temp->next=nullptr;
	if(head==nullptr){
		head=temp;
	}
	else{
		Node*temp1=head;
		while(temp1->next!=nullptr){
/*这里不能写成temp1!=NULL(解释在下面)*/
			temp1=temp1->next;
		}
		temp1->next=temp;
	}
}

在这段代码中,temp1 是一个指向链表中节点的指针。在 while 循环中,我们使用 temp1 检查当前节点的 next 是否为空,如果为空,表示当前节点是链表中的最后一个节点,我们应该在其后插入新节点。如果我们将条件改为 temp != NULL,则会导致问题。
temp1 是最后一个节点时,temp1->nextNULL。如果我们用 temp != NULL 作为条件,那么在最后一个节点的时候,temp1 将会是 NULL,这样循环将退出,并且我们会将新节点插入到空指针后面,而不是最后一个节点的后面。这将导致链表的破坏,因为最后一个节点的 next 应该指向新节点,而不是 NULL
因此,为了正确地在链表的末尾插入新节点,我们必须检查当前节点的 next 是否为空,而不是检查当前节点是否为空。

头插法

//头插法 
void Insert(int x)//在链表头部插入一个值为x的节点
{
	Node*temp=new Node();
	temp->data=x;
	if(head==nullptr){
		temp->next=NULL;
		head=temp;
	}
	else{
		temp->next=head;
		head=temp;
	}
 } 

中间任意插

//中间随机插
void Insert(int x,int p)//在第p个位置插入一个值为x的节点
{
	Node*temp=new Node();
	temp->data=x;

	if(p==1){
		temp->next=head;
		head=temp;	
	}else{
		Node*temp1=head;
		for(int i=0;i<p-2;i++){//找到第p-1个节点
			temp1=temp1->next;
		}
		temp->next=temp1->next;
		temp1->next=temp;
	}
 } 

2、查找一个节点

void Select(int p) {
    Node *temp = head;
    cout << "The num is:";
    for (int i = 0; i < p - 1; i++) {
        temp = temp->next;
    }
    cout << temp->data;
}

3、删除一个节点

void Delete(int p)//删除第p个节点
{
	Node*temp=head;
	if(p==1){
		head=temp->next;
		delete temp;
		return;
	}
	else{
		for(int i=0;i<p-2;i++){
			temp=temp->next;
		}
		Node*temp1=temp->next;
		temp->next=temp1->next;
		delete temp1;
	}
}

4、打印链表

 void Print()//打印整个链表
 {
 	Node*temp=head;
 	cout << "The List is:";
 	while(temp!=nullptr)//这里不能写成temp->next!=nullptr(解释在下面)
 	{
 		cout << temp->data;
 		temp=temp->next;
	 }
	 cout << endl;
 }

我们想要遍历整个链表并打印每个节点的数据。当 tempnullptr时,表示已经到达了链表的末尾,因此循环应该结束。
如果我们将条件改为 temp->next != nullptr,那么循环将会在遍历到链表的倒数第二个节点时结束,而不是遍历到最后一个节点。这是因为在 temp->next != nullptr 的条件下,循环只会继续遍历到最后一个节点的前一个节点,然后退出循环,而最后一个节点的 nextnullptr,因此循环不会再进入最后一个节点。这将导致我们无法打印出链表中最后一个节点的数据。
因此,为了遍历整个链表并打印每个节点的数据,我们应该使用 temp != nullptr 作为循环的条件。

代码实现:
中间随机插(c++)

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node *next;
};

Node *head = nullptr;

// 在指定位置插入节点
void Insert(int x, int p) {
    Node *temp = new Node();
    temp->data = x;

    if (p == 1) {
        temp->next = head;
        head = temp;
    } else {
        Node *temp1 = head;
        for (int i = 0; i < p - 2; i++) {
            temp1 = temp1->next;
        }
        temp->next = temp1->next;
        temp1->next = temp;
    }
}

// 打印整个链表
void Print() {
    Node *temp = head;
    cout << "The List is:";
    while (temp != nullptr) {
        cout << temp->data;
        temp = temp->next;
    }
    cout << endl;
}

// 选择指定位置的节点并打印其值
void Select(int p) {
    Node *temp = head;
    cout << "The num is:";
    for (int i = 0; i < p - 1; i++) {
        temp = temp->next;
    }
    cout << temp->data;
}

// 删除指定位置的节点
void Delete(int p) {
    Node *temp = head;
    if (p == 1) {
        head = temp->next;
        delete temp;
        return;
    } else {
        for (int i = 0; i < p - 2; i++) {
            temp = temp->next;
        }
        Node *temp1 = temp->next;
        temp->next = temp1->next;
        delete temp1;
    }
}

int main() {
    Insert(2, 1);
   	Print();
    Insert(4, 1);
    Print();
    Insert(5, 2);
    Print();
    Select(1);
    cout << endl;
    Delete(2);
    Print();
    return 0;
}

中间随机插(c语言)

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *head = NULL;

// 在指定位置插入节点
void Insert(int x, int p) {
    struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
    temp->data = x;

    if (p == 1) {
        temp->next = head;
        head = temp;
    } else {
        struct Node *temp1 = head;
        for (int i = 0; i < p - 2; i++) {
            temp1 = temp1->next;
        }
        temp->next = temp1->next;
        temp1->next = temp;
    }
}

// 打印整个链表
void Print() {
    struct Node *temp = head;
    printf("The List is:");
    while (temp != NULL) {
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 选择指定位置的节点并打印其值
void Select(int p) {
    struct Node *temp = head;
    printf("The num is:");
    for (int i = 0; i < p - 1; i++) {
        temp = temp->next;
    }
    printf("%d", temp->data);
}

// 删除指定位置的节点
void Delete(int p) {
    struct Node *temp = head;
    if (p == 1) {
        head = temp->next;
        free(temp);
        return;
    } else {
        for (int i = 0; i < p - 2; i++) {
            temp = temp->next;
        }
        struct Node *temp1 = temp->next;
        temp->next = temp1->next;
        free(temp1);
    }
}

int main() {
    Insert(2, 1);
    Print();
    Insert(4, 1);
    Print();
    Insert(5, 2);
    Print();
    Select(1);
    printf("\n");
    Delete(2);
    Print();
    return 0;
}

头插法(c++)
【尾插法实现只需替换下面的Insert函数部分】

#include <iostream>
using namespace std;

struct Node {
    int data;
    Node *next;
};

Node *head = nullptr;

void Insert(int x)//在链表头部插入一个值为x的节点
{
	Node*temp=new Node();
	temp->data=x;
	if(head==nullptr){
		temp->next=nullptr;
		head=temp;
	}
	else{
		temp->next=head;
		head=temp;
	}
 } 

// 打印整个链表
void Print() {
    Node *temp = head;
    cout << "The List is:";
    while (temp != nullptr) {
        cout << temp->data;
        temp = temp->next;
    }
    cout << endl;
}

// 选择指定位置的节点并打印其值
void Select(int p) {
    Node *temp = head;
    cout << "The num is:";
    for (int i = 0; i < p - 1; i++) {
        temp = temp->next;
    }
    cout << temp->data;
}

// 删除指定位置的节点
void Delete(int p) {
    Node *temp = head;
    if (p == 1) {
        head = temp->next;
        delete temp;
        return;
    } else {
        for (int i = 0; i < p - 2; i++) {
            temp = temp->next;
        }
        Node *temp1 = temp->next;
        temp->next = temp1->next;
        delete temp1;
    }
}

int main() {
    Insert(2);
   	Print();
    Insert(4);
    Print();
    Insert(5);
    Print();
    Select(1);
    cout << endl;
    Delete(2);
    Print();
    return 0;
}

头插法(c语言)
【尾插法实现只需替换下面的Insert函数部分】

#include <stdio.h>
#include <stdlib.h>

struct Node {
    int data;
    struct Node *next;
};

struct Node *head = NULL;

void Insert(int x)//在链表头部插入一个值为x的节点
{
	struct Node *temp = (struct Node *)malloc(sizeof(struct Node));
	temp->data=x;
	if(head==NULL){
		temp->next=NULL;
		head=temp;
	}
	else{
		temp->next=head;
		head=temp;
	}
 } 

// 打印整个链表
void Print() {
    struct Node *temp = head;
    printf("The List is:");
    while (temp != NULL) {
        printf("%d", temp->data);
        temp = temp->next;
    }
    printf("\n");
}

// 选择指定位置的节点并打印其值
void Select(int p) {
    struct Node *temp = head;
    printf("The num is:");
    for (int i = 0; i < p - 1; i++) {
        temp = temp->next;
    }
    printf("%d", temp->data);
}

// 删除指定位置的节点
void Delete(int p) {
    struct Node *temp = head;
    if (p == 1) {
        head = temp->next;
        free(temp);
        return;
    } else {
        for (int i = 0; i < p - 2; i++) {
            temp = temp->next;
        }
        struct Node *temp1 = temp->next;
        temp->next = temp1->next;
        free(temp1);
    }
}

int main() {
    Insert(2);
    Print();
    Insert(4);
    Print();
    Insert(5);
    Print();
    Select(1);
    printf("\n");
    Delete(2);
    Print();
    return 0;
}

  • 6
    点赞
  • 8
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值