王道C语言导学班 中级day4

王道C语言导学班 中级day4作业

#define _CRT_SECURE_NO_WARNINGS
#include<stdio.h>
#include<stdlib.h>
/*输入3 4 5 6 7 9999一串整数,
9999代表结束,通过头插法新建链表,并输出,
通过尾插法新建链表并输出。*/
typedef struct Lnode {
	int data;
	struct Lnode* next;
}Lnode,*Linklist;
//尾插法
Linklist creatlist2(Linklist& L) {
	L = (Lnode*)malloc(sizeof(Lnode));
	int a;
	Lnode* p, * q;
	q = L;
	scanf("%d",&a);
	while (a != 9999) {
		p = (Lnode*)malloc(sizeof(Lnode));
		p->data = a;
		q->next = p;
		q = p;
		scanf("%d", &a);
	}
	q->next = NULL;
	return L;
}
//头插法
Linklist creatlist1(Linklist &L) {
	L = (Lnode*)malloc(sizeof(Lnode));
	L->next = NULL;
	int a=0;
	Lnode* p;
	scanf("%d", &a);
	while(a!=9999){
		p = (Lnode*)malloc(sizeof(Lnode));
		
		p->data = a;
		p->next = L->next;
		L->next = p;
		scanf("%d", &a);
	}
	return L;

}
void printlist(Linklist L) {
	L = L->next;
	while (L!=NULL) {
		printf("%3d", L->data);
		L = L->next;
		if (L != NULL) {
			printf(" ");
		}
	}
	printf("\n");
}

int main() {
	Linklist L;
	creatlist1(L);
	printlist(L);
	Linklist P;
	creatlist2(P);
	printlist(P);

}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值