数据结构的问题求大神指教啊
要求是做一个链表,然后把链表的元素转置我自己做了一下,编译没有问题,但是运行的时候一输入元素就出问题#include<stdio.h>#include<stdlib.h>...
要求是做一个链表,然后把链表的元素转置
我自己做了一下,编译没有问题,但是运行的时候一输入元素就出问题
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
char data;
struct node*next;
}linklist;
linklist *head,*p;
linklist *L_create()
{
char ch;
linklist *head,*s,*r;
head=(linklist*)malloc(sizeof(linklist));
r=head;
while((ch=getche())!='#')
{
s=(linklist*)malloc(sizeof(linklist));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
int L_output(linklist*head)
{
linklist*p;
p=head->next;
while(p->next=!NULL)
{
printf("%c",p->data);
p=p->next;
}
return 0;
}
int main()
{
linklist*l,*L;
l=L_create();
L_output(l);
L=L_fun(l);
L_output(L);
return o;
} 展开
我自己做了一下,编译没有问题,但是运行的时候一输入元素就出问题
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
char data;
struct node*next;
}linklist;
linklist *head,*p;
linklist *L_create()
{
char ch;
linklist *head,*s,*r;
head=(linklist*)malloc(sizeof(linklist));
r=head;
while((ch=getche())!='#')
{
s=(linklist*)malloc(sizeof(linklist));
s->data=ch;
r->next=s;
r=s;
}
r->next=NULL;
return head;
}
int L_output(linklist*head)
{
linklist*p;
p=head->next;
while(p->next=!NULL)
{
printf("%c",p->data);
p=p->next;
}
return 0;
}
int main()
{
linklist*l,*L;
l=L_create();
L_output(l);
L=L_fun(l);
L_output(L);
return o;
} 展开
2个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
char data;
struct node*next;
}linklist;
linklist *head,*p;
linklist *L_create()
{
char ch;
int i;
linklist *head,*s,*r;
head=(linklist*)malloc(sizeof(linklist));
while ((ch = getchar()) != '#') {
i += 1;
s = (linklist*)malloc(sizeof(linklist));
s->data = ch;
if (1 == i) {
head = r = s;
} else {
r->next = s;
r = s;
}
}
r->next = NULL;
return head;
}
int L_output(linklist *head)
{
linklist *p;
p = head;
while (p != NULL) {
printf("%c\n", p->data);
p = p->next;
}
return 0;
}
int main()
{
linklist *l, *L;
l = L_create();
L_output(l);
L=L_fun(l);
L_output(L);
return 0;
}
给你改了一下
#include<stdlib.h>
#include<conio.h>
typedef struct node
{
char data;
struct node*next;
}linklist;
linklist *head,*p;
linklist *L_create()
{
char ch;
int i;
linklist *head,*s,*r;
head=(linklist*)malloc(sizeof(linklist));
while ((ch = getchar()) != '#') {
i += 1;
s = (linklist*)malloc(sizeof(linklist));
s->data = ch;
if (1 == i) {
head = r = s;
} else {
r->next = s;
r = s;
}
}
r->next = NULL;
return head;
}
int L_output(linklist *head)
{
linklist *p;
p = head;
while (p != NULL) {
printf("%c\n", p->data);
p = p->next;
}
return 0;
}
int main()
{
linklist *l, *L;
l = L_create();
L_output(l);
L=L_fun(l);
L_output(L);
return 0;
}
给你改了一下
追问
想问一下,原来的代码那里错了,我看懂你的意思了,但是我想把头指针也弄成和后面的结点一样
追答
在你的代码这个L_create函数里, 只是给头指针开辟了空间, 没有赋值啊
没懂你的意思, 可以说详细点
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
/*
uydsh98jhFd#
u y d s h 9 8 j h F d
d F h j 8 9 h s d y u
Press any key to continue
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node {
char data;
struct node *next;
}linklist;
linklist *L_create() {
char ch;
linklist *head,*s,*r;
head = (linklist *)malloc(sizeof(node));
r = head;
while((ch = getche()) != '#') {
s = (linklist *)malloc(sizeof(node));
s->data = ch;
r->next = s;
r = s;
}
r->next = NULL;
return head;
}
void L_output(linklist *head) {
linklist *p;
p = head->next;
printf("\n");
while(p != NULL) {
printf("%c ",p->data);
p = p->next;
}
printf("\n");
}
void L_Reverse(linklist *head) {
linklist *p,*q,*s;
p = NULL;
q = head->next;
while(q) {
s = q->next;
q->next = p;
p = q;
q = s;
}
head->next = p;
}
int main() {
linklist *head = L_create();
L_output(head);
L_Reverse(head);
L_output(head);
return 0;
}
uydsh98jhFd#
u y d s h 9 8 j h F d
d F h j 8 9 h s d y u
Press any key to continue
*/
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node {
char data;
struct node *next;
}linklist;
linklist *L_create() {
char ch;
linklist *head,*s,*r;
head = (linklist *)malloc(sizeof(node));
r = head;
while((ch = getche()) != '#') {
s = (linklist *)malloc(sizeof(node));
s->data = ch;
r->next = s;
r = s;
}
r->next = NULL;
return head;
}
void L_output(linklist *head) {
linklist *p;
p = head->next;
printf("\n");
while(p != NULL) {
printf("%c ",p->data);
p = p->next;
}
printf("\n");
}
void L_Reverse(linklist *head) {
linklist *p,*q,*s;
p = NULL;
q = head->next;
while(q) {
s = q->next;
q->next = p;
p = q;
q = s;
}
head->next = p;
}
int main() {
linklist *head = L_create();
L_output(head);
L_Reverse(head);
L_output(head);
return 0;
}
更多追问追答
追问
想问一下,原来的代码那里错了
追答
L_output()中, while(p->next =! NULL) , 是 !=,不是 =!。
本回答被提问者和网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询