编译没有错误,运行错误
是哪里出了问题
#include <stdio.h>
#include <stdlib.h>
#define N 5
//Node ????
typedef struct node
{
int data;
struct node * next;
}Node;
int main()
{
int creatListFromTail(Node ** pfirst);
void outputList(Node * phead);
void freeList(Node **pfirst);
Node *insertNode(Node * head,int newdata);
Node * phead=NULL;
int x;
if(creatListFromTail(&phead)!=0)
{
outputList(phead);
scanf("%d",&x );
phead=insertNode(phead, x);
outputList(phead);
freeList(&phead);
}
return 0;
}
int creatListFromTail(Node ** pfirst){
int c;
Node*p,*q;
int i;
for(i=1;i<=N;i++){
scanf("%d",&c);
p = (Node*)malloc(sizeof(Node));
p->data = c;
p->next = NULL;
if(i==1){
q = p;
*pfirst = p;
}
else {
q->next = p;
q = p;
}
}
return 1;
}
void outputList(Node * phead){
Node*p;
p = phead;
while(phead){
printf("%d->",p->data);
p = p->next;
}
}
void freeList(Node **pfirst){
Node*p;
Node*q;
q = *pfirst;
while(q){
p = q;
q = q->next;
free(p);
}
}
Node *insertNode(Node * head,int newdata){
Node*p;
Node*q;
p = head;
q = p;
while(1)
{
if(newdata>p->data&&q==p){
p = (Node*)malloc(sizeof(Node));
p->data = newdata;
p->next = head;
break;
}
if(newdata<p->data){
q = p;
p = p->next;
}
else{
p = (Node*)malloc(sizeof(Node));
p->data = newdata;
p->next = q->next;
q->next = p;
break;
}
}
return p;
}
这个函数 改一下 :
void outputList(Node * phead){
Node*p;
p = phead;
while(p){
printf("%d->",p->data);
p = p->next;
}
}
void outputList(Node * phead) 输出链表函数里,while(phead) 应修改为:while(p) ,插入函数:Node *insertNode(Node * head,int newdata) 也存在问题,整体修改如下,供参考:
#include <stdio.h>
#include <stdlib.h>
#define N 5
typedef struct node
{
int data;
struct node * next;
}Node;
int main()
{
int creatListFromTail(Node ** pfirst);
void outputList(Node * phead);
void freeList(Node **pfirst);
Node *insertNode(Node * head,int newdata);
Node * phead=NULL;
int x;
if(creatListFromTail(&phead)!=0)
{
outputList(phead);
printf("\n");
scanf("%d",&x );
phead=insertNode(phead, x);
outputList(phead);
printf("\n");
freeList(&phead);
}
return 0;
}
int creatListFromTail(Node ** pfirst){
int c;
Node*p,*q;
int i;
for(i=1;i<=N;i++){
scanf("%d",&c);
p = (Node*)malloc(sizeof(Node));
p->data = c;
p->next = NULL;
if(i==1){
q = p;
*pfirst = p;
}
else {
q->next = p;
q = p;
}
}
return 1;
}
void outputList(Node * phead){
Node*p;
p = phead;
while(p){ //while(phead) 修改
printf("%d->",p->data);
p = p->next;
}
}
void freeList(Node **pfirst){
Node*p;
Node*q;
q = *pfirst;
while(q){
p = q;
q = q->next;
free(p);
}
}
Node *insertNode(Node * head,int newdata){
Node*p;
Node*q,*t; // 修改
p = head;
q = p;
while(p) // while(1) 修改
{
if(newdata >= p->data){ //if(newdata > p->data && q == p)
//t = (Node*)malloc(sizeof(Node));//p = (Node*)malloc(sizeof(Node));
//t->data = newdata; //p->data = newdata;
//t->next = head; //p->next = head;
break;
}
else { //if(newdata<p->data)
q = p;
p = p->next;
}
}
//if(newdata >= p->data){
t = (Node*)malloc(sizeof(Node));// p = (Node*)malloc(sizeof(Node));
t->data = newdata; //p->data = newdata;
if (head == p){
t->next = head;
head = t;
}
else{
t->next = q->next; //p->next = q->next;
q->next = t; //q->next = p;
}
// break;
//}
return head; //return p;
}
当要求输出第一行第一列时的结果为:当要求输出第0行第2列时的结果为: