简单的链表复习

今天复习链表
先写了一个很low的数字链表,实现想在那个数字前后插入新的节点(静态创建的)

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

struct  Test
{
	int data;
	struct Test *next;
};
void printLink(struct Test *t1)
{
	struct Test *p=t1;
	while(1){
		if(p != NULL){
			printf("%d ",p->data);
			p=p->next;
		}else{
			printf("\n");
			break;
		}	
	}

}
struct Test *inserBehindData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	while(p != NULL){
		if(p->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
struct Test *inserFrontData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	while(p != NULL){
		if(p->next->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
int main()
{
	int a;
	struct Test *head;
	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
	struct Test t3={3,NULL};
	struct Test t4={100,NULL};
	struct Test t5={1001,NULL};
	t1.next=&t2;
	t2.next=&t3;
	head=(struct Test *)malloc(sizeof(struct Test));
	printLink(&t1);
	printf("你想在哪个数字后面插入新的数字\n");
	scanf("%d",&a);
	head=inserBehindData(&t1,a,&t4);//数字后方
	printLink(head);
	a=0;
	printf("你想在哪个数字前面插入新的数字\n");
	scanf("%d",&a);
	head=inserFrontData(&t1,a,&t5);//数字后方
	printLink(head);
	return 0;
}

在这里插入图片描述
问题
如果想要在第一个数字前面加入新的节点,上述代码就有问题
在这里插入图片描述
优化代码

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

struct  Test
{
	int data;
	struct Test *next;
};
void printLink(struct Test *t1)
{
	struct Test *p=t1;
	while(1){
		if(p != NULL){
			printf("%d ",p->data);
			p=p->next;
		}else{
			printf("\n");
			break;
		}	
	}

}
struct Test *inserBehindData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	while(p != NULL){
		if(p->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
struct Test *inserFrontData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	if(p->data == data){//判断是否第一个数就是想要被插入的数
		new1->next=p;
		return new1;
	}
	while(p != NULL){
		if(p->next->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
int witchDataBehind()
{
	int a;
	printf("你想在哪个数字后面插入新的数字\n");
	scanf("%d",&a);
	return a;

}
int witchDataFront()
{
	int a;
	printf("你想在哪个数字前面插入新的数字\n");
	scanf("%d",&a);
	return a;
}
int main()
{
	struct Test *head;
	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
	struct Test t3={3,NULL};
	struct Test t4={100,NULL};
	struct Test t5={1001,NULL};
	t1.next=&t2;
	t2.next=&t3;
	head=(struct Test *)malloc(sizeof(struct Test));
	printLink(&t1);
	head=inserBehindData(&t1,witchDataBehind(),&t4);//数字后方
	printLink(head);
	head=inserFrontData(&t1,witchDataFront(),&t5);//数字后方
	printLink(head);
	return 0;
}

这样就行了,在inserFrontData()函数里面加个判断
在这里插入图片描述
增加计算链表节点个数功能

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

struct  Test
{
	int data;
	struct Test *next;
};
void printLink(struct Test *t1)
{
	struct Test *p=t1;
	while(1){
		if(p != NULL){
			printf("%d ",p->data);
			p=p->next;
		}else{
			printf("\n");
			break;
		}	
	}

}
struct Test *inserBehindData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	while(p != NULL){
		if(p->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
struct Test *inserFrontData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	if(p->data == data){//判断是否第一个数就是想要被插入的数
		new1->next=p;
		return new1;
	}
	while(p != NULL){
		if(p->next->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
int toatlLinkNodeNum(struct Test *t1)
{
	int cnt=0;
	struct Test*p=t1;
	while(p != NULL){
		cnt++;
		p=p->next;

	}
	return cnt;


}
int witchDataBehind()
{
	int a;
	printf("你想在哪个数字后面插入新的数字\n");
	scanf("%d",&a);
	return a;

}
int witchDataFront()
{
	int a;
	printf("你想在哪个数字前面插入新的数字\n");
	scanf("%d",&a);
	return a;
}
void printlength(struct Test *t1)
{
	printf("现在链表节点的长度是:%d\n",toatlLinkNodeNum(t1));
}
int main()
{
	struct Test *head;
	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
	struct Test t3={3,NULL};
	struct Test t4={100,NULL};
	struct Test t5={1001,NULL};
	t1.next=&t2;
	t2.next=&t3;
	head=(struct Test *)malloc(sizeof(struct Test));
	printLink(&t1);
	printlength(&t1);
	head=inserBehindData(&t1,witchDataBehind(),&t4);//数字后方
	printLink(head);
	printlength(head);
	head=inserFrontData(&t1,witchDataFront(),&t5);//数字后方
	printLink(head);
	printlength(head);
	return 0;
}

在这里插入图片描述
增加删除节点的功能

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

struct  Test
{
	int data;
	struct Test *next;
};
void printLink(struct Test *t1)
{
	struct Test *p=t1;
	while(1){
		if(p != NULL){
			printf("%d ",p->data);
			p=p->next;
		}else{
			printf("\n");
			break;
		}	
	}

}
struct Test *inserBehindData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	while(p != NULL){
		if(p->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
struct Test *inserFrontData(struct Test *t1,int data,struct Test *new1)
{
	struct Test*p=t1;
	if(p->data == data){//判断是否第一个数就是想要被插入的数
		new1->next=p;
		return new1;
	}
	while(p != NULL){
		if(p->next->data == data){
			new1->next=p->next;
			p->next=new1;
			return t1;
		}else{
			p=p->next;
		}
	}
	printf("没有找到该数字\n");
	return 0;
}
struct Test *deletNode(struct Test *t1,int data)
{
	struct Test*p=t1;
	if(p->data == data){//判断是否第一个数就是想要删除的节点
		t1=t1->next;
		return t1;
	}
	while(p->next != NULL){
		if(p->next->data == data){
			p->next=p->next->next;
			return t1;
		}
		p=p->next;
		

	}
	return t1;
}
int toatlLinkNodeNum(struct Test *t1)
{
	int cnt=0;
	struct Test*p=t1;
	while(p != NULL){
		cnt++;
		p=p->next;

	}
	return cnt;


}
int witchDataBehind()
{
	int a;
	printf("你想在哪个数字后面插入新的数字\n");
	scanf("%d",&a);
	return a;

}
int witchDataFront()
{
	int a;
	printf("你想在哪个数字前面插入新的数字\n");
	scanf("%d",&a);
	return a;
}
void printlength(struct Test *t1)
{
	printf("现在链表节点的长度是:%d\n",toatlLinkNodeNum(t1));
}
int witchDataDelet()
{
	int a;
	printf("你想删除哪个节点\n");
	scanf("%d",&a);
	return a;
}
int main()
{
	struct Test *head;
	struct Test t1={1,NULL};
	struct Test t2={2,NULL};
	struct Test t3={3,NULL};
	struct Test t4={100,NULL};
	struct Test t5={1001,NULL};
	t1.next=&t2;
	t2.next=&t3;
	head=(struct Test *)malloc(sizeof(struct Test));
	printLink(&t1);
	printlength(&t1);
	head=inserBehindData(&t1,witchDataBehind(),&t4);//数字后方
	printLink(head);
	printlength(head);
	head=inserFrontData(&t1,witchDataFront(),&t5);//数字后方
	printLink(head);
	printlength(head);
	head=deletNode(&t1,witchDataDelet());
	printLink(head);
	printlength(head);
	return 0;
}

在这里插入图片描述
在这里插入图片描述
但是这个是静态创建的属实有点low,要改成动态创建。
代码

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

struct Test
{
	int data;
	struct Test *next;

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

	}
	putchar('\n');

}
struct Test *insertFront(struct Test *head,struct Test *newNode)
{
	
		if(head == NULL){
			head=newNode;
			return head;
		}else{
			newNode->next=head;
			head=newNode;
		}
		
	
		return head;
}
struct Test *createLinkFront(struct Test *head)
{
	
	struct Test *newNode;
	while(1){
		newNode=(struct Test *)malloc(sizeof(struct Test));
		printf("请输入你要插入新节点的数字:\n");
		scanf("%d",&newNode->data);
		if(newNode->data == 0){
			
			return head;
		}
		head=insertFront(head,newNode);
	}
		
		
	
}
int main()
{
	struct Test *head=NULL;
	head=createLinkFront(head);
	printLink(head);

	return 0;
}

非常奇怪,我这代码在VScode 和 sublime text里面跑都是有段错
在这里插入图片描述
在这里插入图片描述
然而在ubuntu里面完美运行
在这里插入图片描述
今天是复习,之前一直在ubuntu上学的链表,今天是在sublime里复习结果这个段错误搞了我大半天,还没搞明白,希望大佬们看见能指导我一下,告诉我原因,谢谢!

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值