輸入并輸出含五個結點的單鏈表,為什么我寫的只能輸入,但不能輸出?求教

uj5u.com熱心網友回復:
create函式里把head = p;改成head->next = p;建議直接貼代碼,還可以幫你除錯一下~
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *link;
}head;
typedef struct node NODK;
void createList(NODK *head,int n){
NODK *p,*q;
int i;
p=(NODK *)malloc(sizeof(NODK));
head=p;
for(i=1;i<=n;i++){
q=(NODK *)malloc(sizeof(NODK));
scanf("%d",&q->data);
q->link=NULL;
p->link=q;
p=q;
}
}
void outputList(NODK *head){
NODK *p;
p=head->link;
while(p->link!=NULL){
printf("%6d",p->data);
p=p->link;
}
}
int main(){
printf("請輸入5個結點的值:");
createList(&head,5);
outputList(&head);
return 0;
}
uj5u.com熱心網友回復:
改了之后有輸出,但是好像是錯誤的
uj5u.com熱心網友回復:
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
struct node{
int data;
struct node *link;
}head;
typedef struct node NODK;
void createList(NODK *head,int n)
{
NODK *p,*q;
int i;
//p=(NODK *)malloc(sizeof(NODK));
//head->link = p;
p = head;
for(i=1;i<=n;i++){
q=(NODK *)malloc(sizeof(NODK));
scanf("%d", &q->data);
q->link=NULL;
p->link=q;
p=q;
}
}
void outputList(NODK *head)
{
NODK *p;
p = head->link;
//while(p->link!=NULL){
while(p){
printf("%6d",p->data);
p=p->link;
}
}
int main(){
printf("請輸入5個結點的值:");
createList(&head,5);
outputList(&head);
return 0;
}
供參考~
uj5u.com熱心網友回復:
資料結構對單鏈表進行資料排序 http://bbs.csdn.net/topics/392201633uj5u.com熱心網友回復:
你的head指向了p(head=p)
p又指向了q(p = q);
鏈表結尾處q-link = null,也就等同于head= null
你的head一直都在改變,所以就沒有輸出了。
uj5u.com熱心網友回復:
或者你可以這樣改,有點兒啰嗦#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *link;
} head;
typedef struct node NODK;
void createList(NODK *head, int n)
{
NODK *p, *q;
int i;
p = (NODK *)malloc(sizeof(NODK));
head->link = p;
for (i = 1; i <= n; i++)
{
q = (NODK *)malloc(sizeof(NODK));
scanf("%d", &q->data);
q->link = NULL;
p->link = q;
p = q;
}
}
void outputList(NODK *head)
{
NODK *p;
p = head->link;
while (p)
{
printf("%6d", p->link->data);
p = p->link;
}
}
int main()
{
printf("input 5 number: ");
createList(&head, 5);
outputList(&head);
return 0;
}
uj5u.com熱心網友回復:
或者你可以這樣改,有點兒啰嗦
#include <stdio.h>
#include <stdlib.h>
#include <malloc.h>
struct node
{
int data;
struct node *link;
} head;
typedef struct node NODK;
void createList(NODK *head, int n)
{
NODK *p, *q;
int i;
p = (NODK *)malloc(sizeof(NODK));
head->link = p;
for (i = 1; i <= n; i++)
{
q = (NODK *)malloc(sizeof(NODK));
scanf("%d", &q->data);
q->link = NULL;
p->link = q;
p = q;
}
}
void outputList(NODK *head)
{
NODK *p;
p = head->link;
while (p)
{
printf("%6d", p->link->data);
p = p->link;
}
}
int main()
{
printf("input 5 number: ");
createList(&head, 5);
outputList(&head);
return 0;
}
好的,萬分感謝,,,
uj5u.com熱心網友回復:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
struct node
{
int data;
struct node *link;
} head;
typedef struct node NODK;
void createList(NODK *head, int n)
{
NODK *p, *q;
int i;
p = head;
// p = (NODK *)malloc(sizeof(NODK));
// head->link = p;
for (i = 1; i <= n; i++)
{
q = (NODK *)malloc(sizeof(NODK));
scanf("%d", &q->data);
q->link = NULL;
p->link = q;
p = q;
}
}
void outputList(NODK *head)
{
NODK *p;
p = head->link;
while (p)
{
printf("%6d", p->data);
p = p->link;
}
}
int main()
{
printf("input 5 number: ");
createList(&head, 5);
outputList(&head);
return 0;
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/houduan/110105.html
標籤:C語言
上一篇:多載輸入運算子
下一篇:vs code怎么用
