链表学习笔记

结构体的定义形式:

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;
}

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

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值