满意答案
ibywm
推荐于 2018.02.14
采纳率:47% 等级:12
已帮助:6414人
#include
#include
#include
//函数结果状态代码
#define OK 1
#define ERROR 0
//Status是函数的类型,其值是函数结果状态代码
typedef int Status;
typedef int ElemType;
//----线性表的单链表存储结构----
typedef struct LNode{
ElemType data;
struct LNode *next;
}LNode, *LinkList;
void CreateList_L(LinkList &L,int n){
//逆位序输入n个元素的值,建立带表头结点的单链线性表L。
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL; //先建立一个带头结点的单链表
for (int i=n;i>0;--i){
LinkList p = (LinkList)malloc(sizeof(LNode)); //生成新结点
scanf("%d",&p->data); //输入元素值
p->next = L->next;L->next = p; //插入到表头
}
}//CreateList_L
Status ListShow_L(LinkList L){
//将带头结点的单链线性链表L的元素列出。
LinkList p = L;
while(p->next){p = p->next;printf("%d ",p->data);}
return OK;
}//ListShow_L
void main(){
int n;
printf("请输入将要载入链表的元素个数:");
scanf("%d",&n); //初始化线性链表元素的个数
printf("请逆序输入%d个元素的值:\n",n);
LinkList L;
CreateList_L(L,n); //初始化线性链表
ListShow_L(L); //输出线性链表元素的值
}
50分享举报