数据结构2.2:单链表

1.代码:

先上代码,后说废话

#define _CRT_SECURE_NO_WARNINGS 1

#include <stdio.h>
#include <malloc.h>

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

/*Initialize the list with header
@return the pointer to the header
*/

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

/*print the list
* @param paraHeader the header of the list.
*/
void printList(NodePtr paraHeader) {
	NodePtr p = paraHeader->next;
	while (p != NULL) {
		printf("%c", p->data);
		p = p->next;
	}
	printf("\r\n");
}

/* Add a element to the tail
* @param paraHeader the header of the list.
* @param paraChar the given char
*/

void addElement(NodePtr paraHeader,char paraChar) {
		NodePtr p, q;
		// Step 1. Construct a new node.
		q = (NodePtr)malloc(sizeof(LNode));
		q->data = paraChar;
		q->next = NULL;

		// Step 2. Search to the tail.
		p = paraHeader;
		while (p->next != NULL) {
			p = p->next;
		}

		// Step 3. Now add/link.
		p->next= q;
}

/*
 * Insert an element to the given position.
 * @param paraHeader The header of the list.
 * @param paraChar The given char.
 * @param paraPosition The given position.
 */

void insertElement(NodePtr paraHeader, char paraChar, int paraPosition) {
	NodePtr p, q;

	//Step 1. Search to the position.
	p = paraHeader;
	for (int i = 0; i < paraPosition; i++) {
		p = p->next;
		if (p == NULL) {
			printf("The position %d is beyond the space of the list. ", paraPosition);
			return;
		}
	}


    //Step 2. Construct a new node.
    q = (NodePtr)malloc(sizeof(LNode));
	q->data = paraChar;

	//Step 3. Now insert q after the p.
	printf("linking\r\n");
	q->next = p->next;
	p->next= q;
}

/*
* Delete an element from the list
* @param paraHeader: the header of the list
* @param paraChar: the given char
*/

void deleteElement(NodePtr paraHeader, char paraChar) {
	NodePtr p, q;
	p = paraHeader;

	//*Search the paraChar
	while ((p->next != NULL) && (p->next->data != paraChar)) {
		p = p->next;
	}// Of while
	if (p->next == NULL) {
		printf("Cannot delete %c", paraChar);
		return;
	}// Of if

	q = p->next;
    p->next = q->next;
    free(q);

	//q = p->next;
	//p->next = q->next;
	//free(p);
}//of deleteElement

/*
* Unit test.
*/

void test() {
	//Step 1. Initialize an empty list 
	LinkList tempList = initLinkList();
	printList(tempList);

	//Step 2. Add some characters
	addElement(tempList, 'H');
	addElement(tempList, 'e');
	addElement(tempList, 'l');
	addElement(tempList, 'l');
	addElement(tempList, 'o');
	addElement(tempList, '!');
	printList(tempList);

	//Step 3. Delete some characters
	deleteElement(tempList, 'e');
	deleteElement(tempList, 'a');
	deleteElement(tempList, 'o');
	printList(tempList);

	//Step 4. Insert to a given position
	insertElement(tempList, 'o', 1);
	printList(tempList);
}//of test

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

2.运行结果


Hello!
Cannot delete aHll!
linking
Holl!

D:\visual studio\LinkNode\x64\Debug\LinkNode.exe (进程 9496)已退出,代码为 0。
要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口. . .

被自己蠢到了,free对象写错找bug找了一个多小时,原代码没删提醒自己是个笨蛋,今天没时间写博客了,有空马上补

 

评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值