IT数码 购物 网址 头条 软件 日历 阅读 图书馆
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
图片批量下载器
↓批量下载图片,美女图库↓
图片自动播放器
↓图片自动播放器↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁
 
   -> 数据结构与算法 -> 链表学习笔记 -> 正文阅读

[数据结构与算法]链表学习笔记

结构体的定义形式:

struct  list_node
{
	int data ; //数据域,用于存储数据
};	//记得加上分号

链表的定义形式

struct  list_node
{
	int data ; //数据域,用于存储数据
	struct list_node *next ; //指针,可以用来访问节点数据,也可以遍历,指向下一个节点
};				//记得加上分号

1.链表和数组
demo1.c

#include <stdio.h>

struct Text
{
        int data;

        struct Text *next;

};

int main()
{
        int i;
        int array[] = {1,2,3,4};
        for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
                printf("%d ",array[i]);                //i是变量遍历数组输出
        }
        putchar('\n');


        struct Text t1 = {1,NULL};						//给结构体赋值
        struct Text t2 = {2,NULL};
        struct Text t3 = {3,NULL};

        t1.next = &t2;
        t2.next = &t3;

        printf("%d %d %d",t1.data,t1.next->data,t1.next->next->data);			//输出t1是结构体的内容用t1.a, t1.next是指针要用->, t1.next->next是指针要用->
        return 0;
}

运行结果
在这里插入图片描述

2.链表静态添加和动态遍历
demo2.c

#include <stdio.h>

struct Text
{
        int data;

        struct Text *next;

};

void printLink(struct Text *head)
{
        struct Text *point = NULL;
        point = head;

        while(1){
                if(point != NULL){
                        printf("%d ",point->data);
                        point = point->next;
                }else{
                        putchar('\n');
                        break;
                }

        }

}

int main()
{
        int i;
        int array[] = {1,2,3,4};
        for(i=0;i<sizeof(array)/sizeof(array[0]);i++){
                printf("%d ",array[i]);                //i是变量遍历数组输出
        }
        putchar('\n');


        struct Text t1 = {1,NULL};					//给结构体赋值
        struct Text t2 = {2,NULL};
        struct Text t3 = {3,NULL};
        struct Text t4 = {5,NULL};
        struct Text t5 = {6,NULL};


        t1.next = &t2;
        t2.next = &t3;
        t3.next = &t4;
        t4.next = &t5;

        printLink(&t1);
        return 0;
}

运行结果
在这里插入图片描述
3.统计链表节点的个数以及链表查找
demo3.c

#include <stdio.h>

struct Text
{
	int data;

	struct Text *next;

};

void printLink(struct Text *head)
{
	struct Text *point = NULL;
	point = head;

	while(1){
		if(point != NULL){
			printf("%d ",point->data);
			point = point->next;
		}else{
			putchar('\n');
			break;
		}
			
	}
		
}

int getTotalNodeNumber(struct Text *head)
{
	int cnt = 0;
	while(1){
		if(head != NULL){
			cnt++;
			head = head->next;
		}else{
			break;
		}
	}
	return cnt;
}

int getSearchData(struct Text *head,int data)
{
	while(head != NULL){
		if(head->data == data){
			printf("find data %d\n",head->data);
			return 1;
		}
		head = head->next;
		
		}
		
	return 0;
}
int main()
{
	int a;

	struct Text t1 = {1,NULL};
	struct Text t2 = {2,NULL};
	struct Text t3 = {3,NULL};
	struct Text t4 = {4,NULL};
	struct Text t5 = {5,NULL};

	
	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;

	printLink(&t1);
	int res = getTotalNodeNumber(&t1);
	printf("number=%d\n",res);
	printf("input search data\n");
	scanf("%d",&a);
	int ret = getSearchData(&t1,a);
	if(ret == 0){
		printf("no find data\n");
	}
	return 0;
}


在这里插入图片描述

在这里插入图片描述
4.链表从指定节点后方插入新节点
在这里插入图片描述

demo4.c

#include <stdio.h>

struct Text
{
	int data;

	struct Text *next;

};

int insertNewNoteFromBhind(struct Text *head,int data,struct Text *new)
{
	struct Text *p = NULL;
	p = head;
	while(p != NULL){
		if(p->data == data){
			new->next = p->next;
			p->next = new;
			return 1;
		}

		p = p->next;
	}
	
	return 0;
}

void printLink(struct Text *head)
{
	struct Text *point = NULL;
	point = head;

	while(point != NULL){

		printf("%d ",point->data);
		point = point->next;
			
	}
		
	putchar('\n');
}


int main()
{
	int a;

	struct Text t1 = {1,NULL};
	struct Text t2 = {2,NULL};
	struct Text t3 = {3,NULL};
	struct Text t4 = {4,NULL};
	struct Text t5 = {5,NULL};


	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	
	struct Text new = {100,NULL};

	printLink(&t1);
	printf("insert data behind\n");
	scanf("%d",&a);
	int ret = insertNewNoteFromBhind(&t1,a,&new);
	if(ret == 1){
		printLink(&t1);
		printf("insert data success\n");
	}else if(ret == 0){
		printf("insert data faile\n");
	}

	return 0;
}

在这里插入图片描述
在这里插入图片描述
5.链表从指定节点前插入新节点
![在这里插入图片描述](https://img-blog.csdnimg.cn/5e90b50cc86d47448f49589c01d10a9a.jpg?x-oss-process=image/watermark,type_ZmFuZ3poZW5naGVpdGk,shadow_10,text_aHR0cHM6Ly9ibG9nLmNzZG4ubmV0L3dlaXhpbl81NTI4NjMwNA==,size_16,color_FFFFFF,t_70
在这里插入图片描述
在之前插入的话要返回给main函数指针head,才能让链表完整的遍历完,所以要用struct Text *指针的形式返回。

demo5.c

#include <stdio.h>

struct Text
{
	int data;

	struct Text *next;

};


struct Text *insertNewNoteFromFor(struct Text *head,int data,struct Text *new)
{
	struct Text *p = head;
	if(p->data == data){
		new->next = p;
		return new;
	}
	while(p->next != NULL){
		if(p->next->data == data){
			new->next = p->next;
			p->next = new;
			return head;
		}
		p = p->next;
	}
	
	return head;
}

void printLink(struct Text *head)
{
	struct Text *point = NULL;
	point = head;

	while(point != NULL){

		printf("%d ",point->data);
		point = point->next;
			
	}
		
	putchar('\n');
}


int main()
{
	int a;

	struct Text t1 = {1,NULL};
	struct Text t2 = {2,NULL};
	struct Text t3 = {3,NULL};
	struct Text t4 = {4,NULL};
	struct Text t5 = {5,NULL};


	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;
	

	struct Text new2 = {99,NULL};

	struct Text *head = &t1;
	printLink(head);

	printf("insert data forward\n");

	scanf("%d",&a);
	head = insertNewNoteFromFor(head,a,&new2);

	printLink(head);
	return 0;
}

运行结果
在这里插入图片描述
在这里插入图片描述
6.链表删除指定节点
在这里插入图片描述
在这里插入图片描述

demo6.c

#include <stdio.h>

struct Text
{
	int data;

	struct Text *next;

};


struct Text *deleteNode(struct Text *head,int data)
{
	struct Text *p = head;
	if(p->data == data){
		head = head->next;
		return head;
	}
	while(p->next != NULL){
		if(p->next->data == data){
			p->next = p->next->next;
			return head;
		}
		p = p->next;
	}
	
	return head;
}

void printLink(struct Text *head)
{
	struct Text *point = NULL;
	point = head;

	while(point != NULL){

		printf("%d ",point->data);
		point = point->next;
			
	}
		
	putchar('\n');
}


int main()
{
	int a;

	struct Text t1 = {1,NULL};
	struct Text t2 = {2,NULL};
	struct Text t3 = {3,NULL};
	struct Text t4 = {4,NULL};
	struct Text t5 = {5,NULL};

	t1.next = &t2;
	t2.next = &t3;
	t3.next = &t4;
	t4.next = &t5;

	struct Text *head = &t1;
	printLink(head);

	printf("delete data\n");

	scanf("%d",&a);
	head = deleteNode(head,a);

	printLink(head);
	return 0;
}

运行结果
在这里插入图片描述
在这里插入图片描述
7.链表动态创建头插法
demo7.c
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
struct Text
{
	int data;
	struct Text *next;

};

void printLink(struct Text *head)
{
	struct Text *p = head;
	while(p != NULL){
		printf("%d ",p->data);
		p = p->next;
	}
	putchar('\n');
}

struct Text *insertNewNoteFor(struct Text *head)
{
	struct Text *new;
	while(1){
		new = (struct Text *)malloc(sizeof(struct Text));
		printf("input data until 0\n");
		scanf("%d",&new->data);
		if(new->data == 0){
			printf("quit\n");
			return head;
		}

		if(head == NULL){
			head = new;
		}else{
			new->next = head; 
			head = new;
		}
	}
	return head;
}
int main()
{

	struct Text *head = NULL;
	head = insertNewNoteFor(head);
	printLink(head);
	return 0;
}

运行结果
在这里插入图片描述
8.链表动态创建尾插法
demo8.c
在这里插入图片描述

#include <stdio.h>
#include <stdlib.h>
struct Text
{
	int data;
	struct Text *next;

};

void printLink(struct Text *head)
{
	struct Text *p = head;
	while(p != NULL){
		printf("%d ",p->data);
		p = p->next;
	}
	putchar('\n');
}

struct Text *insertNewNoteBehind(struct Text *head,struct Text *new)
{
	struct Text *p = head;
	if(head == NULL){
		head = new;
		return head;		//记得return不然运行到下一行会发生段错误
	}
	while(p->next != NULL){
		p = p->next;
	}
	p->next = new;
	return head;
	

}
struct Text *createNote(struct Text *head)
{
	struct Text *new;
	while(1){
		new = (struct Text *)malloc(sizeof(struct Text));
		printf("input data until 0\n");
		scanf("%d",&new->data);
		if(new->data == 0){
			printf("quit\n");
			return head;
		}
		head = insertNewNoteBehind(head,new);
	}
}

int main()
{

	struct Text *head = NULL;
	struct Text t1 = {1000,NULL};
	head = createNote(head);
	printLink(head);


	return 0;
}

运行结果
在这里插入图片描述

  数据结构与算法 最新文章
【力扣106】 从中序与后续遍历序列构造二叉
leetcode 322 零钱兑换
哈希的应用:海量数据处理
动态规划|最短Hamilton路径
华为机试_HJ41 称砝码【中等】【menset】【
【C与数据结构】——寒假提高每日练习Day1
基础算法——堆排序
2023王道数据结构线性表--单链表课后习题部
LeetCode 之 反转链表的一部分
【题解】lintcode必刷50题<有效的括号序列
上一篇文章      下一篇文章      查看所有文章
加:2021-08-12 16:53:24  更:2021-08-12 16:56:55 
 
开发: C++知识库 Java知识库 JavaScript Python PHP知识库 人工智能 区块链 大数据 移动开发 嵌入式 开发工具 数据结构与算法 开发测试 游戏开发 网络协议 系统运维
教程: HTML教程 CSS教程 JavaScript教程 Go语言教程 JQuery教程 VUE教程 VUE3教程 Bootstrap教程 SQL数据库教程 C语言教程 C++教程 Java教程 Python教程 Python3教程 C#教程
数码: 电脑 笔记本 显卡 显示器 固态硬盘 硬盘 耳机 手机 iphone vivo oppo 小米 华为 单反 装机 图拉丁

360图书馆 购物 三丰科技 阅读网 日历 万年历 2024年5日历 -2024/5/21 5:18:16-

图片自动播放器
↓图片自动播放器↓
TxT小说阅读器
↓语音阅读,小说下载,古典文学↓
一键清除垃圾
↓轻轻一点,清除系统垃圾↓
图片批量下载器
↓批量下载图片,美女图库↓
  网站联系: qq:121756557 email:121756557@qq.com  IT数码