c语言链表问题,链表创建的不合适,能运行,输出结果只有最后一个输入
#include<stdio.h>#include<stdlib.h>structstu{intnum;structstu*next;}temp;voidinput(st...
#include<stdio.h>
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
}temp;
void input(struct stu*s,int x)
{
struct stu*p,*p1;;
p=s->next;
p->num=x;
p->next=NULL;
p1=(struct stu*)malloc(sizeof(temp));
p->next=p1;
p=p1;
p1->next=NULL;
}
void main()
{
int n,i,x;
struct stu *head,*s;
head=(struct stu*)malloc(sizeof(temp));
head->next=NULL;
head->next=(struct stu*)malloc(sizeof(temp));
printf("聊表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("输入第%d个数",i+1);
scanf("%d",&x);
input(head,x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s->next!=NULL)
{ printf("%d->",s->num);
s=s->next;
if(s->next==NULL)printf("\b\b");
}
} 展开
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
}temp;
void input(struct stu*s,int x)
{
struct stu*p,*p1;;
p=s->next;
p->num=x;
p->next=NULL;
p1=(struct stu*)malloc(sizeof(temp));
p->next=p1;
p=p1;
p1->next=NULL;
}
void main()
{
int n,i,x;
struct stu *head,*s;
head=(struct stu*)malloc(sizeof(temp));
head->next=NULL;
head->next=(struct stu*)malloc(sizeof(temp));
printf("聊表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{ printf("输入第%d个数",i+1);
scanf("%d",&x);
input(head,x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s->next!=NULL)
{ printf("%d->",s->num);
s=s->next;
if(s->next==NULL)printf("\b\b");
}
} 展开
展开全部
#include<stdio.h>
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
};
typedef struct stu node;
void input(struct stu *s,int x)
{
node *p;
p=(node *)malloc(sizeof(node));
p->num=x;
p->next = s->next;
s->next = p;
}
void main()
{
int n,i,x;
node *head,*s;
head=(node *)malloc(sizeof(node));
head->next=NULL;
printf("链表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("输入第%d个数",i+1);
scanf("%d", &x);
input(head, x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s!=NULL)
{
printf("%d->",s->num);
s=s->next;
if(s==NULL)printf("\b\b");
}
}
修改成了头插法
#include<stdio.h>
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
};
typedef struct stu node;
void input(struct stu *s,int x)
{
node *cur;
while(s->next)s = s->next;
cur = (node *)malloc(sizeof(node));
cur->num = x;
cur->next = NULL;
s->next = cur;
}
void main()
{
int n,i,x;
node *head,*s;
head=(node *)malloc(sizeof(node));
head->next=NULL;
printf("链表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("输入第%d个数",i+1);
scanf("%d", &x);
input(head, x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s!=NULL)
{
printf("%d->",s->num);
s=s->next;
if(s==NULL)printf("\b\b");
}
}
这是尾插法
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
};
typedef struct stu node;
void input(struct stu *s,int x)
{
node *p;
p=(node *)malloc(sizeof(node));
p->num=x;
p->next = s->next;
s->next = p;
}
void main()
{
int n,i,x;
node *head,*s;
head=(node *)malloc(sizeof(node));
head->next=NULL;
printf("链表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("输入第%d个数",i+1);
scanf("%d", &x);
input(head, x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s!=NULL)
{
printf("%d->",s->num);
s=s->next;
if(s==NULL)printf("\b\b");
}
}
修改成了头插法
#include<stdio.h>
#include<stdlib.h>
struct stu
{
int num;
struct stu *next;
};
typedef struct stu node;
void input(struct stu *s,int x)
{
node *cur;
while(s->next)s = s->next;
cur = (node *)malloc(sizeof(node));
cur->num = x;
cur->next = NULL;
s->next = cur;
}
void main()
{
int n,i,x;
node *head,*s;
head=(node *)malloc(sizeof(node));
head->next=NULL;
printf("链表元素的个数:");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("输入第%d个数",i+1);
scanf("%d", &x);
input(head, x);
}
getchar();
printf("打印链表:");
s=head->next;
while(s!=NULL)
{
printf("%d->",s->num);
s=s->next;
if(s==NULL)printf("\b\b");
}
}
这是尾插法
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询