单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素+指针,元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
创建单链表
typedef struct LinkNode{
char data;
struct LinkNode *next;
} L,*NodePtr;
初始化
NodePtr iniLinkList(){
NodePtr Header = (NodePtr)malloc(sizeof(L));
Header->data = 0;
Header->next = NULL;
return Header;
}
打印单链表
void printList(NodePtr pList){
NodePtr p = pList->next;
while(p != NULL){
printf("%c",p->data);
p = p->next;
}
printf("\n");
}
尾部追加
void appendElement(NodePtr pList,char pchar){
NodePtr p,q;
//创建新节点
p = (NodePtr)malloc(sizeof(L));
p->data = pchar;
p->next = NULL;
//找最后一个
q = pList;
while(q->next != NULL){
q = q->next;
}
q->next = p;
}
插入新结点
void insertElement(NodePtr pList,char pchar,int pos){
NodePtr p,q;
p = pList;
for(int i = 0;i < pos;i++){
p = p->next;
if(p == NULL){
printf("第%d个位置超出表范围",pos);
return;
}
}
q = (NodePtr)malloc(sizeof(L));
q->data = pchar;
q->next = NULL;
//printf("linking\n");
q->next = p->next;
p->next = q;
}
删除结点
void deleteElement(NodePtr Header,char pchar){
NodePtr p,q;
p = Header;
while((p->next != NULL) && (p->next->data != pchar)){
p = p->next;
}
if(p->next == NULL){
printf("无法删除\n");
return;
}
q = p->next;
p->next = p->next->next;
free(q);
}
测试函数
void appendinsertdeleteTest(){
NodePtr tempList = iniLinkList();
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, 'e', 1);
insertElement(tempList, 'o', 4);
deleteElement(tempList, '!');
insertElement(tempList, ',', 5);
insertElement(tempList, 'W', 6);
insertElement(tempList, 'o', 7);
insertElement(tempList, 'r', 8);
insertElement(tempList, 'l', 9);
insertElement(tempList, 'd', 10);
insertElement(tempList, '!', 11);
printList(tempList);
}
主函数
int main(){
appendinsertdeleteTest();
return 0;
}
运行结果
Hello!
无法删除
Hll!
Hello,World!
--------------------------------
完整代码
#include <stdio.h>
#include <malloc.h>
//创建单链表
typedef struct LinkNode{
char data;
struct LinkNode *next;
} L,*NodePtr;
//初始化
NodePtr iniLinkList(){
NodePtr Header = (NodePtr)malloc(sizeof(L));
Header->data = 0;
Header->next = NULL;
return Header;
}
//打印表
void printList(NodePtr pList){
NodePtr p = pList->next;
while(p != NULL){
printf("%c",p->data);
p = p->next;
}
printf("\n");
}
//尾部追加
void appendElement(NodePtr pList,char pchar){
NodePtr p,q;
//创建新结点
p = (NodePtr)malloc(sizeof(L));
p->data = pchar;
p->next = NULL;
//找最后一个
q = pList;
while(q->next != NULL){
q = q->next;
}
q->next = p;
}
//插入新结点
void insertElement(NodePtr pList,char pchar,int pos){
NodePtr p,q;
p = pList;
for(int i = 0;i < pos;i++){
p = p->next;
if(p == NULL){
printf("第%d个位置超出表范围",pos);
return;
}
}
q = (NodePtr)malloc(sizeof(L));
q->data = pchar;
q->next = NULL;
//printf("linking\n");
q->next = p->next;
p->next = q;
}
//删除结点
void deleteElement(NodePtr Header,char pchar){
NodePtr p,q;
p = Header;
while((p->next != NULL) && (p->next->data != pchar)){
p = p->next;
}
if(p->next == NULL){
printf("无法删除\n");
return;
}
q = p->next;
p->next = p->next->next;
free(q);
}
//测试
void appendinsertdeleteTest(){
NodePtr tempList = iniLinkList();
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, 'e', 1);
insertElement(tempList, 'o', 4);
deleteElement(tempList, '!');
insertElement(tempList, ',', 5);
insertElement(tempList, 'W', 6);
insertElement(tempList, 'o', 7);
insertElement(tempList, 'r', 8);
insertElement(tempList, 'l', 9);
insertElement(tempList, 'd', 10);
insertElement(tempList, '!', 11);
printList(tempList);
}
int main(){
appendinsertdeleteTest();
return 0;
}