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);
}
测试结果