69,373
社区成员
发帖
与我相关
我的任务
分享
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
#define OK 1
#define OVERFLOW -2
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode,*LinkList;
//创建链表
//Status CreateList(LinkList &L,int n)
Status CreateList(LinkList *L,int n) /*注意改动*/
{
int i,j; /*注意改动*/
(*L)=(LinkList)malloc(sizeof(LNode));/*注意改动*/
if(!(*L))exit(ERROR); /*注意改动*/
(*L)->data=n /*注意改动*/
(*L)->next=NULL; /*注意改动*/
LinkList p=*L /*注意改动*/
LinkList q=*L; /*注意改动*/
printf("Please input numbers:\n");
for(i=1;i<n;i++)
{
p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&j); /*注意改动*/
p->data=j; /*注意改动*/
p->next=q; /*注意改动*/
q=p; /*注意改动*/
}
*L=p; /*注意改动*/
return OK;
}
///输出元素
Status PrintList(LinkList L)
{
printf("The numbers are:");
while(L) /*注意改动*/
{
//L=L->next;
printf("%d ",L->data);
L=L->next;
}
printf("\n");
return OK;
}
//主函数
int main()
{
int n;
// ElemType e;
LinkList L=NULL;
printf("Please input the number you need:");
scanf("%d",&n);
CreateList(&L,n); /*注意改动*/
PrintList(L);
system("pause");
return 0;
}
#include<stdio.h>
#include<stdlib.h>
#define OK 1
#define OVERFLOW -2
#define ERROR 0
typedef int ElemType;
typedef int Status;
typedef struct LNode{
ElemType data;
struct LNode *next;
} LNode, *LinkList;
///创建链表
Status CreateList(LinkList &L,int n)
{
int i;
LinkList p, q;
L=(LinkList)malloc(sizeof(LNode));
if(!L)
exit(ERROR);
L->data=n;
L->next=NULL;
printf("Please input numbers:\n");
for(i=0;i<n;i++)
{
//scanf("%d",&L->data);
//L=(LinkList)malloc(sizeof(LNode));
//L->next=L;
p=(LinkList)malloc(sizeof(LNode));
scanf("%d",&p->data);
p->next = L->next;
L->next=p;
}
return OK;
}
///输出元素
Status PrintList(LinkList L)
{
printf("The numbers are:");
//while(L->next)
while(L)
{
printf("%d ",L->data);
L=L->next;
//printf("%d ",L->data);
}
printf("\n");
return OK;
}
//主函数
int main()
{
int n;
ElemType e;
LinkList L;
printf("Please input the number you need:");
scanf("%d",&n);
CreateList(L,n);
PrintList(L);
system("pause");
}