带头结点的单链表

#include<bits/stdc++.h>
using namespace std;
typedef struct LNode{
	int data;
	struct LNode* next;
}LNode,*LinkList;
int InitList(LinkList& L){
	L=(LNode*)malloc(sizeof(LNode));
	if(L==NULL){
		return false;
	}
	L->next=NULL;
	return true;
}
void IsEmpty(LinkList& L){
	printf("%s",L->next==NULL?"链表为空":"");
}

void InsertList(LinkList& L){//前插法 
	L->next=NULL;
	int x;
	while(x!=9999){
	LNode* p=(LNode*)malloc(sizeof(LNode));
	cin>>x;
	p->data=x;
	p->next=L->next;
	L->next=p;
	}
}

void InsertList1(LinkList& L){//后插 
	int x;
	LNode *p6;
	p6=L;
	while(x!=9999){
		LNode* p1=(LNode*)malloc(sizeof(LNode));
		cin>>x;
		p1->data=x;
		p1->next=p6->next;
		p6->next=p1;
		p6=p1;
	}
}

void BianLiList(LinkList L){
	LNode* q=L->next;
	for(q=q;q!=NULL;q=q->next){
		cout<<"输出元素为:"<<q->data<<endl;
	}
}
int main(){
	LinkList L;
	InitList(L);
	IsEmpty(L);
	InsertList(L);
	BianLiList(L);
	return 0;
}

在这里插入图片描述
在这里插入图片描述

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值