今天复习链表
先写了一个很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里复习结果这个段错误搞了我大半天,还没搞明白,希望大佬们看见能指导我一下,告诉我原因,谢谢!