c语言(双链表)

1 结构定义

typedef struct DLNode {
	int data;
	struct DLNode* prior;
	struct DLNode* next;
} DLNode;

2 链表操作

  • 头节点插入
void add2head(DLNode* L, int x) {
	DLNode* p = (DLNode*)malloc(sizeof(struct DLNode));
	p->data = x;
	p->next = L->next;
	p->prior = L;
	L->next->prior = p;
	L->next = p;
}
  • 在第k个位置插入
void insert_k(DLNode* L, int k, int x) {
	int cnt = 0;
	DLNode* p;
	p = L;
	while (p ->next != NULL && cnt++ <= k - 1) p = p->next;
	DLNode* q = (DLNode*)malloc(sizeof(struct DLNode));
	q->data = x;
	q->next = p->next;
	q->prior = p;
	if (p ->next != NULL)p->next->prior = q;
	p->next = q;
}
  • 删除操作
// 删除第k位置的数
void remove(DLNode* &L, int k) {
	DLNode* p, * q;	
	p = L;
	int cnt = 0;
	if (k == 1) {
		q = L;
		L = L->next;
		L->next->prior = L;
		free(q);
	}
	else {
		while (p->next != NULL && cnt++ <= k - 1) p = p->next;
		q = p->next;
		p->next = q->next;
		q->next->prior = p;
		free(q);
	}
}
  • 打印操作
void printList(DLNode* L) {
	if (L == NULL) return;
	printf("current list is : \n");
	DLNode* p = L -> next;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
}
  • 查找某个元素是否存在
bool exist(DLNode *L, int x) {
	DLNode* p;
	p = L;
	while (p->next != NULL && p->next->data != x) p = p->next;
	return p ->next != NULL;
}

3 测试代码

#include <stdio.h>
#include <stdlib.h>
const int N = 100;
typedef struct DLNode {
	int data;
	struct DLNode* prior;
	struct DLNode* next;
} DLNode;
void createDlist(DLNode*& L, int a[], int n) {
	DLNode* s, * r;
	L = (DLNode*)malloc(sizeof(struct DLNode));
	L->next = NULL;
	L->prior = NULL;
	r = L;
	for (int i = 0; i < n; i++) {
		s = (DLNode*)malloc(sizeof(DLNode));
		s->data = a[i];
		s->next = NULL;
		s->prior = r;
		r->next = s;
		r = s;
	}
	r->next = NULL;
}
void printList(DLNode* L) {
	if (L == NULL) return;
	printf("current list is : \n");
	DLNode* p = L -> next;
	while (p != NULL)
	{
		printf("%d ", p->data);
		p = p->next;
	}
}
void add2head(DLNode* L, int x) {
	DLNode* p = (DLNode*)malloc(sizeof(struct DLNode));
	p->data = x;
	p->next = L->next;
	p->prior = L;
	L->next->prior = p;
	L->next = p;
}
void insert_k(DLNode* L, int k, int x) {
	int cnt = 0;
	DLNode* p;
	p = L;
	while (p ->next != NULL && cnt++ <= k - 1) p = p->next;
	DLNode* q = (DLNode*)malloc(sizeof(struct DLNode));
	q->data = x;
	q->next = p->next;
	q->prior = p;
	if (p ->next != NULL)p->next->prior = q;
	p->next = q;
}
void remove(DLNode* &L, int k) {
	DLNode* p, * q;	
	p = L;
	int cnt = 0;
	if (k == 1) {
		q = L;
		L = L->next;
		L->next->prior = L;
		free(q);
	}
	else {
		while (p->next != NULL && cnt++ <= k - 1) p = p->next;
		q = p->next;
		p->next = q->next;
		q->next->prior = p;
		free(q);
	}
}
bool exist(DLNode *L, int x) {
	DLNode* p;
	p = L;
	while (p->next != NULL && p->next->data != x) p = p->next;
	return p ->next != NULL;
}
int a[N];
int main() {
	int n;
	printf("enter len : \n");
	scanf_s("%d", &n);
	printf("enter %d nums to init()\n", n);
	for (int i = 0; i < n; i++) scanf_s("%d", &a[i]);
	DLNode* L;
	createDlist(L, a, n);
	printList(L);
	int op;
	while (true) {
		scanf_s("%d", &op);
		if (op == 1) {
			int x;
			printf("add to head : \n");
			scanf_s("%d", &x);
			add2head(L, x);
			printList(L);
		} 
		if (op == 2) {
			printf("add to k : \n");
			int k, x;
			scanf_s("%d%d", &k, &x);
			insert_k(L, k, x);
			printList(L);
		} 
		if (op == 3) {
			printf("remove k : \n");
			int k;
			scanf_s("%d", &k);
			remove(L, k);
			printList(L);
		}
		if (op == 4) {
			int x;
			scanf_s("%d", &x);
			if (exist(L, x)) printf("Yes");
			else printf("No");
		}
	}
	return 0;
}

注:此篇代码只是为了应付考试,很多边界和代码优化没有做,后续有时间补上。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值