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找了一个多小时,原代码没删提醒自己是个笨蛋,今天没时间写博客了,有空马上补