数据结构:单链表的基本操作(带头结点)C语言

直接附上代码,代码中的注释已经很清楚了!

(创建文件时,选择  .cpp文件, 因为代码中用到了C++的知识)

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

/*[1].自定义数据元素的数据类型*/
typedef int ElemType;

/*[2].单链表的定义*/
typedef struct LNode{
	ElemType data;
	struct LNode *next;
} LNode, *LinkList;   // LNode 头结点,*LinkList 是头指针,头指针指向头结点,就是指向整个链表 


/*[3].单链表的初始化*/
/*<1>.带头结点的单链表的初始化*/
bool InitList(LinkList &L) {
	L = (LNode *)malloc(sizeof(LNode));
	if (L==NULL) {
		return false;
	}
	L->next = NULL;
	return true;
}

/*遍历链表打印输出结果*/
bool PrintList(LinkList &L) {
	LNode *p = L;
	while(p->next != NULL) {
		p = p->next;
		printf("%d-->", p->data);
	}
	printf("NULL\n");
	return true;
} 

/*[4].头插法建立单链表   -->  逆向建立单链表*/
bool List_HeadInsert(LinkList &L) {
	LNode *new_node;
	ElemType new_node_data;   // 新结点的data
	
	printf("please input a number(end 9999):");
	scanf("%d", &new_node_data);
	
	while(new_node_data != 9999) {
		new_node = (LNode *)malloc(sizeof(LNode)); // 为新结点分配内存空间 
		
		new_node->data = new_node_data;
		new_node->next = L->next;
		L->next = new_node;
		
		printf("please input a number(end 9999):");
		scanf("%d", &new_node_data);
	} 
	
	return true;
} 

/*[5].尾插法建立单链表   ->   正向建立单链表*/
bool List_TailInsert(LinkList &L) {
	LNode *new_node;  // 指向新结点的指针 
	ElemType new_node_data;  // 新结点的data 
	LNode *tail_node = L; // 指向尾结点的指针,一开始是指向头结点的,随着插入新结点,不断移动尾指针指向新插入的结点 
	
	printf("please input a number InsertTail(end 9999):");
	scanf("%d", &new_node_data);
	
	while (new_node_data != 9999) {
		new_node = (LNode *)malloc(sizeof(LNode));
		if (new_node == null) {
			return false;
		}
		new_node->data = new_node_data;
//		new_node->next = NULL;
		tail_node->next = new_node;
		
		tail_node = new_node;  // 将尾指针指向新插入到尾部的结点 
		
		printf("please input a number InsertTail(end 9999):");
		scanf("%d", &new_node_data);
	}
	
	tail_node->next = NULL;  // 不满足条件,将尾指针置空
	return true; 
} 


/*[6].单链表的按位查找*/
LNode *GetElem(LinkList &L, int i) {
	if (i < 0) {
		printf("the elem does not exist!\n"); 
	}
	int j = 0;  // 记录当前结点的位序,头结点是第0个结点 
	LNode *p = L;
	while(p->next != NULL && j<i) {
		p = p->next; 
		j++; 
	}
	return p;
} 

/*[7].单链表的按值查找*/
LNode *LocateElem(LinkList &L, ElemType e) {
	if (!L->next) {
		printf("the LinkList is NULL!\n"); 
		return L;
	}
	LNode *p = L;
	while(p->next != NULL) {
		p = p->next;
		if (p->data == e) {
			return p;
		} 
	}
	printf("the LinkList's elem that you are looking for does not exist!\n");
	return NULL;
} 


/*[8].按位插入*/
bool InsertElem(LinkList &L, int i, ElemType e) {
	if (i < 1) {
		return false;
	} 
	LNode *p = L;
	int j = 0; // 记录当前结点的位序
	while(p->next != NULL && j < i-1) {
		p = p->next;
		j++;
	}
	
	LNode *new_node = (LNode *)malloc(sizeof(LNode));
	new_node->data = e;
	new_node->next = p->next;
	p->next = new_node;
	
	return true; 	
} 

/*[8].按位删除*/
bool DeleteElem(LinkList &L, int i, ElemType &e) { // &e将删除的元素带出 
	if (!L->next)  {
		printf("需要删除的链表为空\n");
		return false;
	} 
	LNode *p=L;  // 指向待删除结点的前驱结点 
	int j = 0;
	while (p->next!=NULL && j < i-1) {
		p = p->next;
		j++;
	} 
	LNode *q;  // 指向待删除的结点
	q = p->next;
	p->next = q->next;
	e = q->data;
	
	free(q);  // 释放q 
	
	return true;
}


/*[9].销毁单链表*/ 
bool DestoryList(LinkList &L) {
	int e;
	while(L->next) {
		DeleteElem(L, 1, e);
		PrintList(L);
	}
	free(L);
}


/*[10].求单链表的表长*/
int ListLength(LinkList &L) {
	int len = 0;
	LNode *p = L;
	while (p->next != NULL) {
		p=p->next;
		len++;
	}
	return len;
}


int main() {
	LinkList L1;
	InitList(L1);

	/*1.头插法测试*/
    List_HeadInsert(L1);

	/*2.尾插法测试*/
	List_TailInsert(L1); 
	PrintList(L1);
	
	/*3.按位查找测试*/
	LNode *getNum = GetElem(L1, 4);
	printf("\n按位查找到的元素是:%d\n", getNum->data);
	
	/*4.按值查找测试*/
	LNode *locateElem = LocateElem(L1, 3);
	printf("\n按值查找找到的元素是:%d\n",locateElem->data);
	
	/*5.按位插入测试*/
	InsertElem(L1, 3, 5);
	PrintList(L1);
	printf("\n");
	
	/*6.按位删除测试*/
	ElemType e; 
	DeleteElem(L1, 3, e);
	PrintList(L1);
	printf("\n");
	
	/*7.销毁单链表测试*/
	DestoryList(L1); 
	
	return 0;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

是小蟹呀^

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值