单链表——代码解析

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

typedef struct LNode {
	int   data;       //数据域 
	struct LNode *next;   //指针域
} LNode ;

// 1.链表的初始化 (创建了一个空表)
LNode *InitList_L1() {
	LNode *head = (LNode*)malloc(sizeof(LNode)); //定义一个头指针并让其指向头节点 
	head->next = NULL;//初始化为空表 
	return head;
}
// 1.链表的初始化 (创建了一个非空表)
//LinkList InitList_L2()
LNode *InitList_L2() {
	LNode *head,*p1,*p2,*p3,*p4;
	head = (LNode*)malloc(sizeof(LNode)); //定义一个头指针并让其指向头节点
	p1 = (LNode*)malloc(sizeof(LNode));
	p2 = (LNode*)malloc(sizeof(LNode));
	p3 = (LNode*)malloc(sizeof(LNode));
	p4 = (LNode*)malloc(sizeof(LNode));
	p1->data = 12;
	p2->data = 43;
	p3->data = 7;
	p4->data = 90;
	head->next = p1;
	p1->next = p2;
	p2->next = p3;
	p3->next = p4;
	p4->next = NULL;
	return head;
}

// 2. 求表长 : 返回 L中数据元素个数  
int ListLength_L(LNode *head) {
	int i=0;
	LNode *p;
	p = head->next;  //p指向第一个结点(首元节点)
	while(p) { //遍历单链表,统计结点数
		i++;
		p=p->next;
	}
	return i;
}
// 3. 判断表是否为空 
int ListEmpty(LNode *head) {  //LinkList head 
//若L为空表,则返回1,否则返回0
	if(head->next)   //非空 
		return 0;
	else
		return 1;
}
// 4. 获取线性表L中的某个数据元素的内容 
int GetElem_L(LNode *head,int i) {
	LNode *p;
	int j=1;//计数器 
	p = head->next;  // p指向了首元节点
	while(p) {
		if(j == i){
			return p->data;
		}
		else
		{
			p = p->next;
			j++;
		}
	}
}//GetElem_L

//5.在线性表 L 中查找值为 e 的数据元素
int LocateELem_L (LNode *head , int e) {
//返回L中值为e的数据元素的位置序号,查找失败返回0
	LNode *p = head->next;//
	int j=1;
	while(p) {
		if(p->data == e){
			return j;
		}
		else{
			p = p->next;
			j++;
		} 
	}
	if(p==NULL) return 0;
}

// 6. 在 L中第 i个元素之前插入数据元素 e
LNode *ListInsert_L(LNode *head,int i,int e) {
	LNode *p,*p1,*p2;
	int j = 1;
	p1 = head;//;让p1始终在p2的前一步 
	p2 = head->next;//冲锋陷阵,从首元结点开始 
	p = (LNode*)malloc(sizeof(LNode));
	p->data = e;
	p->next = NULL;
	while(p2) {
		if(i==j) {
			p->next = p2;
			p1->next = p;
			break;
		}
		p1 = p2;
		p2 = p2->next;
		j++;
	}
	return head;
}//ListInsert_L


// 7. 打印链表
void print_L(LNode *head) {
	LNode *p;
	p = head->next;
	while(p) {
		printf("%d -> ",p->data);
		p = p->next;
	}
	printf("\n");
}

// 8.将线性表L中第i个数据元素删除 
void ListDelete_L(LNode *head,int i) {
	LNode *p1,*p2;
	p1 = head;
	p2 = head->next;
	int j = 1;
	while(p2){
		if(j==i){
			p1->next = p2->next;
			free(p2);
			break;
		}
		p1 = p2;
		p2 = p2->next;
		j++;
	}
}//ListDelete_L

//9. 尾插法建立链表 
//正位序输入n个元素的值,建立带表头结点的单链表L  
LNode *CreateList_L(LNode *head , int n){ 
	LNode *r,*p;
	int e;
	head = (LNode*)malloc(sizeof(LNode));//让头指针指向头节点 
	head->next = NULL;// 
	r = head;//让尾指针也指向头节点 
	for(int i=1;i<=n;i++){
		scanf("%d",&e);
		p = (LNode*)malloc(sizeof(LNode));//指向新生成的结点 
		p->data = e;
		p->next = NULL;
		r->next = p;
		r = p;
	} 
	return head;
}//CreateList_L 

// 10.链表逆转
LNode *rever(LNode *head){
	int len = ListLength_L(head);
	LNode *p = head->next;
	int a[100];
	for(int i=1;i<=len;i++){
		a[i] = p->data;
		p = p->next;
	}
	p = head->next;
	for(int i=len;i>=1;i--){
		p->data = a[i];
		p = p->next;
	}
	return head;
}
 
int main() {
	LNode *head;
//	head = InitList_L2();//创建了一个非空表
//	print_L(head);
//	head = ListInsert_L(head,3,666);
//	print_L(head);
//	ListDelete_L(head,2);
//	print_L(head);
	head = CreateList_L(head , 6);
	print_L(head);
	rever(head);
	print_L(head);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

朱事顺利、

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

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

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

打赏作者

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

抵扣说明:

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

余额充值