提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档
前言——单链表的定义
单链表是一种链式存取的数据结构,用一组地址任意的存储单元存放线性表中的数据元素。链表中的数据是以结点来表示的,每个结点的构成:元素(数据元素的映象) + 指针(指示后继元素存储位置),元素就是存储数据的存储单元,指针就是连接每个结点的地址数据。
提示:以下是本篇文章正文内容,下面案例可供参考
一、单链表的逻辑结构示意图
二、单链表的实际操作
1.定义单链表
/**
* Linked list of characters. The key is data.
*/
typedef struct LinkNode
{
char data;
struct LinkNode *next;
}LNode,*LinkList,*NodePtr;
2.创建单链表
/**
* Initialize the list with a header.
*@return The pointer to the header.
*/
LinkList initLinkList()
{
NodePtr tempHeader =(NodePtr)malloc(sizeof(LNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
return tempHeader;
}
3.打印单链表
/**
* 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");
}
4.插入
/**
* Add an element to the tail.
*@param paraHeader The header of the list.
*@param paraChar The given char.
*/
void appendElement(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;
}
5.指定位置插入
/**
*Insert an element to the given position.
*@param paraHeader The header of the list.
*@param paraChar The given char.
*@param paraPositon The given position.
*/
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\r\n");
q->next = p->next;
p->next = q;
}
6.删除指定元素
/**
*Delet 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;
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);
}
完整代码
#include <stdio.h>
#include <malloc.h>
/**
* Linked list of characters. The key is data.
*/
typedef struct LinkNode
{
char data;
struct LinkNode *next;
}LNode,*LinkList,*NodePtr;
/**
* Initialize the list with a 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 an element to the tail.
*@param paraHeader The header of the list.
*@param paraChar The given char.
*/
void appendElement(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 paraPositon The given position.
*/
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\r\n");
q->next = p->next;
p->next = q;
}
/**
*Delet 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;
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);
}
/**
* Unit test.
*/
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);
printf("删去刚才的输入'!'\n");
deleteElement(tempList,'!');
printList(tempList);
printf("删去刚才输入的'e','o'\n");
deleteElement(tempList,'e');
deleteElement(tempList,'o');
printList(tempList);
printf("在'H'后添加'a'\n");
insertElement(tempList,'a',1);
printList(tempList);
}
int main()
{
appendInsertDeleteTest();
}