2017年武术套路冠军:高手进!急~~等待答案中!!

来源:百度文库 编辑:高校问答 时间:2024/04/30 04:52:30
下面的程序哪里错了!!
望哪位高人指出!!我用的是win-tc编译器
#include<stdio.h>
#include<conio.h>
struct list
{
int data;
struct list * next;
};
typedef struct list node;
node * creat();
node * deletelist(node *);

int main()
{
node * head,* tail;
head = creat();
head = deletelist(head);
for(tail = head->next;tail!=NULL;tail = tail->next)
printf(" %d ", tail->data);
getch();
return 0;
}

node * creat()
{
node * head,* p,* tail;
int a;
head = (node *)malloc(sizeof (node));
head->next = NULL;
tail = head;
scanf("%d", &a);
while(a!=0)
{
p = (node *)malloc(sizeof (node));
p->next = NULL;
p->data = a;
tail->next = p;
tail = tail->next;
scanf("%d", &a);
}
return head;
}

node * deletelist(head)
{
node * p;
int n;
scanf("%d", &n);
for(p = head->nex;p!=NULL;p = p->next)
{
if(n == p->next->data)
{
p->next = p->next->next;
}
}
return head;
}

少一个头函数文件#include<stdlib.h>,这里有错误node * deletelist(head) ,我已经给你改了,你看看好了没有?
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct list
{
int data;
struct list * next;
};
typedef struct list node;
node * creat();
node * deletelist(node *);

int main()
{
node * head,* tail;
head = creat();
head = deletelist(head);
for(tail = head->next;tail!=NULL;tail = tail->next)
printf(" %d ", tail->data);
getch();
return 0;
}

node * creat()
{
node * head,* p,* tail;
int a;
head = (node *)malloc(sizeof (node));
head->next = NULL;
tail = head;
scanf("%d", &a);
while(a!=0)
{
p = (node *)malloc(sizeof (node));
p->next = NULL;
p->data = a;
tail->next = p;
tail = tail->next;
scanf("%d", &a);
}
return head;
}

node * deletelist(node * head)
{
node * p;
int n;
scanf("%d", &n);
for(p = head->next;p!=NULL;p = p->next)
{
if(n == p->next->data)
{
p->next = p->next->next;
}
}
return head;
}

好象IF语句少个分号
node * deletelist(head)
{
node * p;
int n;
scanf("%d", &n);
for(p = head->nex;p!=NULL;p = p->next)
{
if(n == p->next->data)
;
{
p->next = p->next->next;
}
}
return head;
}

deletelist有错误
逻辑错,你有头结点,p赋值head,再拿n与p->next->data比较,而不是赋head->next,这样会跳过了链表中有用的第一个结点

node * deletelist(node *head)
{
node * p;
int n;
scanf("%d", &n);
for(p = head;p!=NULL;p = p->next)
{
if(n == p->next->data)
{
p->next = p->next->next;
}
}
return head;
}