C语言高手帮忙啊!急!!!!!!!!

作者&投稿:徒律 (联系方式在网页底部的电邮联系)
C语言高手帮忙~

#include
void main(){
void eliminate(int n, int k);
int total, num;
printf("请输入总人数:
");
scanf("%d",&total);
printf("请输入淘汰号k:
");
scanf("%d",&num);
eliminate(total,num);
}
void eliminate(int n, int k){
int *arr, size=n, count=-1;
arr=new int[n];
for(int i=0; i<n; i++){
arr[i]=i+1;//这里可以用输入语句scanf("%d",&a[i]);来手动输入每个参与者的编号
}
while(size>1){
for(int i=0;i<k;i++){
count++;
if(count==size)
count=0;
}
size--;
printf("第 %d 个淘汰者是 %d 号
",n-size,arr[count]);
if(count!=size)
for(int i=count;i<=size;i++)
arr[i]=arr[i+1];
else
count=0;
}
printf("幸存者是 %d 号
",arr[0]);
}

p = a 改成
p=&a[0][0];

问题很严重,表面上是第4行和第9行有错误,但实质并非如此。
错误:
1:第3行,类型定义后面应加上分号,注意与define不同
2:main()函数,在调用CREAT()、LENGTH()和OUT()函数时不应加上linklist、int和void,你这样写是在申明函数,并非调用。
3:你创建的链表是一第一个元素为头节点的的,但你却将链表长度赋给头节点,导致原头节点值丢失,无法输出。
还有很多指针运用上面的错误,我就不多说了,下面是我修改过的程序:
#include<alloc.h>
#include<stdio.h>
typedef int datatype ;
typedef struct node
{
datatype data;
struct node *next;
} linklist;
linklist *CREAT()
{
datatype n;
linklist * head,* s,* r;
head=NULL;
r=NULL;
printf("Please input the numbers!");
scanf("%d",&n);
head=(linklist*)malloc(sizeof(linklist));
head->data=0;
r=head;
while(n!=0)
{
s=(linklist*)malloc(sizeof(linklist));
s->data=n;
r->next=s;
r=s;
scanf("%d",&n);
}
r->next=NULL;
return head;
}

int LENGTH(linklist *L)
{
int i=0;
linklist *p,*head=L;
p=head->next;
while(p!=NULL)
{
p=p->next;
i++;
}
head->data=i;
return(i);

}

void OUT(linklist *L)
{
linklist *p, *head=L;
p=head->next;
printf("The length of the linklist is %d! \n", head->data);
while(p!=NULL)
{
printf("%d,",p->data);
p=p->next;
}
}

void main()
{
linklist *L;
L=CREAT();
LENGTH(L);
OUT(L);
}
该程序在turboc2.0上运行成功。
顺便说一下:其实我这学期刚学《数据结构》,我想你也应该在学这门课吧,不知道可否交个朋友,以后好互相讨教。QQ:729430705

1. CREAT()函数里面头节点head申明了但没有在链表种使用它 到最后返回head那么链表肯定是空
2.typedef int datatype 后面是不是没加;

#include<malloc.h>
#include<stdio.h>
typedef int datatype ;
typedef struct node
{
datatype data;
struct node *next;
} *linklist;
linklist CREAT()
{
datatype n;
linklist head,s, r;
head=(linklist)malloc(sizeof(linklist));
head->next=NULL;
printf("Please input the numbers!");
scanf("%d",&n);
while(n!=0)
{
s=(linklist)malloc(sizeof(linklist));
s->data=n;
s->next=head->next;
head->next=s;
scanf("%d",&n);
}
//s->data=n;
//r->next=NULL;
return head;
}

int LENGTH(linklist L)
{
int i=0;
linklist p,head;
p=L->next;
while(p!=NULL)
{
p=p->next;
i++;
}
L->data=i;
return(i);

}

void OUT(linklist L)
{
linklist p ,head;
p=head=L;
printf("The length of the linklist is %d! \n", head->data);
while(p!=NULL)
{
printf("%d__",p->data);
p=p->next;
}
}

void main()
{
linklist L;
int length;
L= CREAT();
length=LENGTH(L);
OUT(L);
}

这个程序...即使按照上面那样改过来,估计也运行不了!!!!

照你程序的意思头结点不算元素节点,只是用来存放节点个数对吗?
但是你输入的时候确实将它当做了存放元素的节点了!

你是不是输入的时候一直到输入0才停止输入呀?那0不算节点元素是嘛?如果是就按照下面我给你该的看看是不是你要的:

#include<malloc.h>
#include<stdio.h>
typedef int datatype;
typedef struct node
{
datatype data;
struct node *next;
} linklist;

node *MakeNode(int data) /*节点生成单独提出来作为一个函数*/
{
node *p=(node *)malloc(sizeof(node));
p->data = data;
p->next = NULL;
return p;
}

linklist *initLink()/*链表初始化*/
{
node *p=MakeNode(0);/*输出化的点吧元素个数为0,应传个0*/
return p;
}

int scanData()
{
datatype n;
printf("Please input the numbers:");
scanf("%d",&n);
return n;
}
linklist *CREAT()
{
datatype n=-1;
node *head,*cur,*p;
p= head = initLink();/*初始化链表*/
n = scanData();
while(n)
{
cur = MakeNode(n);
p->next=cur; /*当前节点插入链表末尾节点*/
p=cur;/*当前节点成为链表末尾节点*/

head->data++;/*节点数加1*/

n = scanData();
}
return head;
}

int LENGTH(linklist *L)
{
return L->data;
}

void OUT(linklist *L)
{
/*
linklist *p,*head;
p=head; /*这里的head又是一个野指针*/

linklist *cur=L->next;
printf("The length of the linklist is: %d \n", L->data);
while(cur) /*输出的时候只要判断当前节点是否还有下一个节点不就行了?*/
{
printf("%3d",cur->data);
cur=cur->next;
}
printf("\n");
}

void main()
{
linklist *L;
L = CREAT();
LENGTH(L);
OUT(L);
}

相关视频

C语言高手帮忙啊!急!!!
1:第3行,类型定义后面应加上分号,注意与define不同 2:main()函数,在调用CREAT()、LENGTH()和OUT()函数时不应加上linklist、int和void,你这样写是在申明函数,并非调用。3:你创建的链表是一第一个元素为头节点的的,但你却将链表长度赋给头节点,导致原头节点值丢失,无法输出。还有很多指针

全国二级C模拟题目4,请教C语言高手,急!!!
最后,代码执行`else`后面的`printf`语句,因此结果输出为`0`。综上所述,当`a`的初始值为`-1`时,执行给定代码段的结果为`0`。

求C语言高手帮忙!!! 输入n个数字,把它们反过来输出——但是不能用循环...
void main(){ int n,i;char *p;printf("Please input n:\\n");scanf("%d",&n);p=(char *)malloc((n+1)*sizeof(char));\/\/由于是用字符数组,故多分一个空间存n与接下来n个数字之间的回车或者空格键;for(i=0;i<n+1;i++)scanf("%c",p+i);\/\/*p应该为回车符'\\n'或者空格符...

C语言高手帮忙啊!
struct point c;c.x=a.x-b.x;c.y=a.y-b.y;return c;} float triangle(struct point A, struct point B, struct point C){ float a,b,c,p,area;struct point d,e,f;d=sub(A,B);e=sub(A,C);f=sub(B,C);c=sqrt(d.x*d.x+d.y*d.y);b=sqrt(e.x*e.x+e.y*e....

c语言高手帮帮忙!
0,0,0,0};int total = 0;char input[256] = "\\0";char* name[5] = {"张","王","李","赵","废票"};while (true) {printf("输入1选张,输入2选王,输入3选李,输入4选赵,输入-1结束投票,输入其他值为废票\\n");scanf("%s", &input);if (strcmp(input,"1") == 0) {++...

C语言的问题 请高手帮忙回答 谢谢~!~急···
肯定有15 while (i<15)if(++i%3!=0) continue;else printf(“%3d”,i);} 当i=14时,进入循环,先执行++i进行if判断,此时i变成15,if条件不满足(15%3==0),执行else,打印15 谁

C语言编程求助!!!求高手帮忙!感激不尽!
include "stdio.h"include"string.h"int main(){ int i,s = 0; \/*请修改此处:int和i间有空格; 计算的和值s首先要清零 *\/ char str[80];i=0;while ((str[i]=getchar())!='\\n')i++;str[i]='\\0';for(i=0;i<80;i++)if (str[i]>='0' && str[i]<='9') \/*...

C语言的几道考题,请各位高手帮忙,急!!!(追加50分)
三题我答案和你的一样 四题 五题的S=0没什么作用,可以不写,最后的GETCH也可不写,我是没发现有什么用。六题 include<stdio.h> int stu(int a,int b,int c);int stu(int a,int b,int c){ int s;s=a*a+b*b+c*c;return s;} void main(){ int a,b,c;printf("请输入3个...

c语言的高手,帮个忙啊,本人现在没有财富值了,呜呜。。
1、先进行括号内运算,--b就是8,d++值没有变,所以(--b==d++)为真,即a=1,所以执行真的条件d++ 2、先进行两个括号内的运算a>b为假,m=0。c>d为假,n=0。然后0交0为0所以k为0 3、char *p[] 其实是一个二维数组 其中p[0]存储的是“Chinese”,p[1]=“France",p[2]=“America”,p

C语言高手帮忙做这三道题,要详解 谢谢拉!!
1、A while()是判断括号里的是真还是假,若表达式为0,就是假,若表达式非0,就是真。"!"是非逻辑运算符,简单说就是把非0变成0,把0变成1。。。"while(!x)”就是"(!x)"取真是进行循环,就是"!x"的值为非0时,就是"x"的值为0时。所以就是x==0.2、C 这题和上题一样,就是(m...