数据结构(C/C++)--单链线性表

#include<stdio.h>
#include<stdlib.h>

#define MaxSize 100

typedef int ElemType;

typedef struct LNode {
	ElemType data;
	struct  LNode * next;
}LNode ,*LinkeList ;

bool InitLIst(LinkeList& L) {
	L = (LNode*)malloc(sizeof(LNode));
	if (L == NULL) return false;
	L->next = NULL;
	return true;
}

//头插法
bool HeadInsert(LinkeList& L) {
	int x;
	scanf("%d", &x);
	while (x != 9999) {
		LNode* s = (LNode*)malloc(sizeof(LNode));
		s->data = x;
		s->next = L->next;
		L->next = s;
		scanf("%d", &x);
	}
	return true;
}

//前插法 在节点p之前插入e
bool ListpreInsert(LNode * p,ElemType e) {
	if (p == NULL) return false;
	LNode* s = (LNode*)malloc(sizeof(LNode));
	if (s == NULL) return false;
	s->next = p->next;
	p->next = s;
	s->data = p->data;
	p->data = e;
	return true;;
}

//按位插入
bool ListInsert(LinkeList &L, int i, ElemType e) {
	if (i < 1) return false;
	LNode* P;
	int j = 0;
	P = L;
	while (P != NULL && j < i - 1) {
		P = P->next;
		j++;
	}

	LNode* S = (LNode*)malloc(sizeof(LNode));
	if (P == NULL) return false;
	S->data = e;
	S->next = P->next;
	P->next = S;
	return true;
}

//按位删除
bool ListDelet(LinkeList& L, int i, ElemType e) {
	if (i < 0) return false;
	LNode* p;
	p = L;
	int j = 0;
	while (p!=NULL&&j<i-1){
		p = p->next;
	}
	if (p == NULL) return false;
	if (p->next == NULL) return false;
	LNode* s = p->next;
	e = s->data;
	p->next = s->next;
	free(s);
	return true;
}

//删除指定结点
bool DeletNode(LNode* p) {
	if (p == NULL) return false;
	if (p->next == NULL) {
		p = p->next;
		return true;
	}
	p->next = p->next->next;
	p->data = p->next->data;	
	free(p->next);
	return true;
}

//按位查找
LNode* GetElem(LinkeList L, int i) {
	LNode* p;
	p = L;
	int j = 0;
	while (p != NULL && j < i ) {
		p = p->next;
	}
	return p;
}

//按值查找
LNode* LocateElem(LinkeList L, ElemType e) {
	LNode* p=L->next;
	while (p->next!=NULL&&p->data!=e){
		p = p->next;
	}
	return p;
}

//打印线性表
bool ShowLNode(LinkeList L) {
	if (L == NULL) return false;
	LNode* p = L->next;
	while (p!= NULL) {
		printf("%d\t", p->data);
		p = p->next;
	}
	return true;
}

int main() {
	LNode* L;
	InitLIst(L);
	HeadInsert(L);
	ShowLNode(L);
	ListInsert(L, 2, 66);
	ShowLNode(L);
	return 1;
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值