目录
1、下面的代码为什么在ubuntu里编译没问题,但在啊哈c中编译就有问题
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Test {
int data;
struct Test *next;
};
void printLink(struct Test *head) {
struct Test *point;
point = head;
while (point != NULL) {
printf("%d ", point->data);
point = point->next;
}
putchar('\n');
}
struct Test *insertDynamicHead(struct Test *head) {
struct Test *new;
while (1) {
new = (struct Test *)malloc(sizeof(struct Test));//寮€杈熺┖闂? printf("input your new node data:\n");
scanf("%d", &(new->data));
if (new->data == 0) {
printf("0 quit\n");
return head;
}
if (head == NULL) {
head = new;
} else {
new->next = head;
head = new;
}
}
return head;
}
int main() {
int i = 3;
struct Test *head = NULL;
head = insertDynamicHead(head);
printLink(head);
system("pause");
return 0;
}
解答要加一句NULL的初始化
2、指针的指针与数组的关系
#include <stdio.h>
#include <stdlib.h>
int main()
{
int **a;
int i;
// int b[3][2] = {{1,2},{3,4},{5,9}};
a = (int **)malloc(sizeof(int *)*16); //开辟**a的空间
int aa[3];
a[0]=(int*)calloc(3, sizeof(int)); //注意二维数组要给行也开辟空间
// a[1]=(int*)calloc(3, sizeof(int));
//printf("%d",*(a+1));
for(i =0;i<2;i++)
{
scanf("%d",&aa[i]);
a[0][i] = aa[i];
a[1][i] = aa[i];
//printf("%d",a[0][i]);
}
for( i= 0;i<2;i++)
{
printf("%d",a[1][i]);
}
/*int a = 10;
int *b =&a;
b =20;
printf("%d",*b);*/
system("pause");
return 0;
}
3、链表中的节点每k个一组翻转代码出现了错误
#include <stdio.h>
#include <stdlib.h>
struct lod{
int a;
struct lod *next;
struct lod *pot;
};
void *puts1(struct lod *head)
{
struct lod * lode = head;
struct lod * new;
int a;
while(1)
{
new = (struct lod *)malloc(sizeof(struct lod));
new->next = NULL;
printf("请给个输入值:并且以0结束\n");
scanf("%d",&a);
new->a = a;
if(new->a == 0)
{
break;
}
new->next =lode;
lode =new;
}
return lode;
}
void printf1(struct lod*p)
{
while(p != NULL)
{
printf("%d",p->a);
p=p->next;
}
}
void *fanzhuan(struct lod *head,int a) //链表的翻转
{
struct lod *net=NULL;
struct lod *len=NULL;
struct lod *p=NULL;
int i=0;
while(head !=NULL) //a为k个值的翻转头插法
{
// printf("/");
if(i<a)
{
len =head->next;
head->next=net;
net = head;
head=len;
i++;
}else{
break;
}
}
p=net;
while(net != NULL) //打印net翻转后的前a个值的链表翻转
{
printf("%d",net->a);
net=net->next;
}
//net =head;
while(1) //出现问题,想利用尾插法将后面的几个是原封不动的加入到链表的尾部但不行
{
printf("sadfsdfsa\n");
if(head == NULL)
{
break;
}
head=head->next;
net->next =head;
net=head;
printf("*****%d",net->a);
//net=net->next;
// printf("sadfsdfsa");
}
//net->next =NULL;
net =p;
printf("++++++\n");
while(net != NULL) //打印只有翻转的部分这是为什么
{
printf("%d",net->a);
net=net->next;
}
// printf("sadfsdfsa");
return p;
}
int main()
{
struct lod *haod =NULL;
int a=3;
haod =puts1(haod);
printf1(haod);
printf("-------------------\n");
haod = fanzhuan(haod,a);
//printf1(haod);
system("pause");
return 0;
}
解决:主要是尾插法的原因吧
#include <stdio.h>
#include <stdlib.h>
struct lod{
int a;
struct lod *next;
struct lod *pot;
};
void *puts1(struct lod *head)
{
struct lod * lode = head;
struct lod * new;
int a;
while(1)
{
new = (struct lod *)malloc(sizeof(struct lod));
new->next = NULL;
printf("请给个输入值:并且以0结束\n");
scanf("%d",&a);
new->a = a;
if(new->a == 0)
{
break;
}
new->next =lode;
lode =new;
}
return lode;
}
void printf1(struct lod*p)
{
while(p != NULL)
{
printf("%d",p->a);
p=p->next;
}
}
void *fanzhuan(struct lod *head,int a) //链表的翻转
{
struct lod *net=NULL;
struct lod *len=NULL;
struct lod *p=NULL;
struct lod *p1=NULL;
int i=0;
while(head !=NULL) //a为k个值的翻转头插法
{
// printf("/");
if(i<a)
{
len =head->next;
head->next=net;
net = head;
head=len;
i++;
}else{
break;
}
}
p=net;
while(net->next != NULL) //打印net翻转后的前a个值的链表翻转,解决想实现尾部插入要直接net->next 做判断
{
printf("%d",net->a);
net=net->next;
}
// net->next =head;
printf("/******%d******/",net->a);
while(1)
{
printf("sadfsdfsa\n");
net->next =head;
net=head;
head=head->next;
if(head == NULL)
{
break;
}
printf("*****%d",net->a);
//net=net->next;
// printf("sadfsdfsa");
}
//net->next =NULL;
net =p;
printf("++++++\n");
while(net != NULL)
{
printf("%d",net->a);
net=net->next;
}
printf(".........................\n");
while(p1 != NULL)
{
printf("%d",p1->a);
p1=p1->next;
}
// printf("sadfsdfsa");
return p;
}
int main()
{
struct lod *haod =NULL;
int a=3;
haod =puts1(haod);
printf1(haod);
printf("-------------------\n");
haod = fanzhuan(haod,a);
//printf1(haod);
system("pause");
return 0;
}
4、什么时候需要传递二级指针?
答:https://www.cnblogs.com/AndyJee/p/4630153.html
这篇文章解释的非常的全
5、NC24 删除有序链表中重复的元素有点缺陷待补全,三个或以上的相同数。
#include <stdio.h>
#include <stdlib.h>
struct headop{
int a;
struct headop *next;
};
void *put(struct headop* net)
{
net=(struct headop*)malloc(sizeof(struct headop));
net->next=NULL;
net->a=5;
struct headop *a1;
struct headop *beet=net;
int i=1;
while(1)
{
a1= (struct headop*)malloc(128);
printf("请输入第 %d 个数\n",i);
i++;
scanf("%d",&a1->a);
if(a1->a==0)
{
break;
}
beet->next=a1;
beet =beet->next;
printf("+++%d++\n",a1->a);
// net=(struct headop*)malloc(sizeof(struct headop));
//net->next=NULL;
}
beet->next=NULL;
return net;
}
void *chazhao(struct headop *sin)
{
struct headop* s=sin;
struct headop* p=sin;
struct deadop* a=sin;
sin=sin->next;
while(1)
{
//sin=sin->next;
if(s->a == sin->a)
{
p->next=sin->next;
s=sin->next;
sin=sin->next->next;
}else{
s=s->next;
sin=sin->next;
p=p->next;
}
if(sin ==NULL)
{
break;
}
}
return a;
}
int main()
{
struct headop* head;
// head=(struct headop*)malloc(sizeof(struct headop));
struct headop* len;
head=put(head);
len=head;
while(head != NULL)
{
printf("%d-->",head->a);
head=head->next;
}
printf("\n");
len=chazhao(len);
while(len != NULL)
{
printf("%d-->",len->a);
len=len->next;
}
printf("请输入链表");
system("pause");
return 0;
}