数据结构第二天——单链表

1.创建

typedef struct LinkNode{
	char data;
	struct LinkNode *next;
} LNode, *LinkList, *NodePtr;

2.初始化有头结点的链表

LinkList initLinkList(){
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader;
}

3.添加元素到链表尾部

void appendElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;
	
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	q->next = NULL;
	
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}
	
	p->next = q;
} 

4. 插入

void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
	NodePtr p, q;
	
	p = paraHeader;
	int i;
	for (i = 0; i < paraPosition; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}
	}
	
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	
	printf("linking\n");
	q->next = p->next;
	p->next = q;
}

5. 删除

void deleteElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;
	
	p = paraHeader;
	while ((p->next != NULL) && (p->next->data != paraChar)){
		p = p->next;
	}
	
	if (p->next == NULL) {
		printf("Cannot delete %c\n", paraChar);
		return;
	}
	
	q = p->next;
	p->next = p->next->next;
	free(q);
}

 6.测试

void appendInsertDeleteTest(){
	LinkList tempList = initLinkList();
	printList(tempList);
	
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	appendElement(tempList, '!');
	printList(tempList);
	
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);
	
	insertElement(tempList, 'o', 1);
	printList(tempList);
}

总代码

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

typedef struct LinkNode{
	char data;
	struct LinkNode *next;
} LNode, *LinkList, *NodePtr;

/**
 *初始化有头结点的链表
 */

LinkList initLinkList(){
	NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
	tempHeader->data = '\0';
	tempHeader->next = NULL;
	return tempHeader;
}

/**
 *打印链表
 */
 
void printList(NodePtr paraHeader){
	NodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;
	}
	printf("\n");
} 

/**
 *添加元素到链表
 */
 
void appendElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;
	
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	q->next = NULL;
	
	p = paraHeader;
	while (p->next != NULL) {
		p = p->next;
	}
	
	p->next = q;
} 

/**
 *插入元素到链表
 */
 
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
	NodePtr p, q;
	
	p = paraHeader;
	int i;
	for (i = 0; i < paraPosition; i ++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the scope of the list.", paraPosition);
			return;
		}
	}
	
	q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;
	
	printf("linking\n");
	q->next = p->next;
	p->next = q;
} 

/**
 *删除一个数组里的元素
 */
 
void deleteElement(NodePtr paraHeader, char paraChar){
	NodePtr p, q;
	
	p = paraHeader;
	while ((p->next != NULL) && (p->next->data != paraChar)){
		p = p->next;
	}
	
	if (p->next == NULL) {
		printf("Cannot delete %c\n", paraChar);
		return;
	}
	
	q = p->next;
	p->next = p->next->next;
	free(q);
}

void appendInsertDeleteTest(){
	LinkList tempList = initLinkList();
	printList(tempList);
	
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	appendElement(tempList, '!');
	printList(tempList);
	
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);
	
	insertElement(tempList, 'o', 1);
}


int main(){
	appendInsertDeleteTest();
	
	return 0;
} 

运行结果

7.insertElement函数更多测试

void appendInsertDeleteTest(){
	LinkList tempList = initLinkList();
	printList(tempList);
	
	appendElement(tempList, 'H');
	appendElement(tempList, 'e');
	appendElement(tempList, 'l');
	appendElement(tempList, 'l');
	appendElement(tempList, 'o');
	appendElement(tempList, '!');
	printList(tempList);
	
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);
	
	insertElement(tempList, 'o', 1);
	printList(tempList);
	deleteElement(tempList, 'o');
	
	insertElement(tempList, 'e', 1);
	insertElement(tempList, 'o', 4);
	insertElement(tempList, ' ', 5);
	insertElement(tempList, 'W', 6);
	insertElement(tempList, 'o', 7);
	insertElement(tempList, 'r', 8);
	insertElement(tempList, 'l', 9);
	insertElement(tempList, 'd', 10);
	printList(tempList);
}

 测试结果

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值