1.代码如下
#include <stdio.h>
#include <malloc.h>
/**
* define the structure and the pointer to the structure
*/
typedef struct LinkNode
{
char data;
struct LinkNode *next;
} LNode , *LinkList, *NodePtr;
/**
* initialize the list
*/
LinkList initLinkList()
{
NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
tempHeader->data='\0';
tempHeader->next=NULL;
return tempHeader;
}
/**
* print 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 node to the tail of the list
*/
void appendElement(NodePtr paraHeader,char paraChar)
{
NodePtr p,q;
//step 1. creat a new node
q=(NodePtr)malloc(sizeof(LNode));
q->data=paraChar;
q->next=NULL;
//step 2. find the tail
p=paraHeader;
while(p->next!=NULL)
{
p=p->next;
}
//step 3. add the node to the tail
p->next=q;
}
/**
* insert an element to the specific position
*/
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition)
{
NodePtr p,q;
//step 1. find the specific position
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 ;
}
}
//step 2. create a node
q=(NodePtr)malloc(sizeof(LNode));
q->data=paraChar;
//step 3.insert the node to the list
printf("linking\r\n");
q->next=p->next;
p->next=q;
}
/**
* delete an element from the list
*/
void deleteElement(NodePtr paraHeader, char paraChar)
{
NodePtr p,q;
p=paraHeader;
//step 1. try to find the element
while((p->next!=NULL)&&(p->next->data!=paraChar))
{
p=p->next;
}
//step 2. if not find the element
if(p->next==NULL)
{
printf("Cannot delete %c\r\n",paraChar);
return ;
}
//step 3. delete the node and free the node
q=p->next;
p->next=p->next->next;
free(q);
}
/**
* test the functions above
*/
void appendInsertDeleteTest()
{
//step 1. creat a new list
LinkList tempList =initLinkList();
printList(tempList);
//step 2. fill the list
appendElement(tempList,'H');
appendElement(tempList,'e');
appendElement(tempList,'l');
appendElement(tempList,'l');
appendElement(tempList,'o');
appendElement(tempList,'!');
printList(tempList);
//step 3. delete some characters
deleteElement(tempList,'e');
deleteElement(tempList,'a');
deleteElement(tempList,'o');
printList(tempList);
//step 4. insert an element to the specific position
insertElement(tempList,'o',1);
printList(tempList);
}
/**
* output the address to konw more
*/
void basicAddressTest()
{
LNode tempNode1, tempNode2;
tempNode1.data=4;
tempNode1.next=NULL;
tempNode2.data=6;
tempNode2.next=NULL;
printf("The first node: %d, %d, %d\r\n",&tempNode1, &tempNode1.data, &tempNode1.next);
printf("The second node: %d, %d, %d\r\n",&tempNode2, &tempNode2.data, &tempNode2.next);
tempNode1.next=&tempNode2;
}
int main()
{
appendInsertDeleteTest();
basicAddressTest();
}
2.图示插入和删除操作
3.代码说明
1)该代码是带头节点的单链表,带头节点与不带头节点相比的好处是:删除节点时不用判断第一个节点是否满足条件,后续部分代码也相对好写。
2)用指针p来寻找某一个特定节点位置时应该使用p->next来进行判断,这样就能使得插入和删除时的前一个节点,插入(删除)节点,后一个节点成为一个整体,能够进行p->next,p->next->next操作。
3)在进行插入节点操作时,先让前一个node的next赋给插入node的next,再将插入node的地址赋给前一个节点的next,这样就不会使前一个node的next被替代,无法将后一个节点的地址传递
4)在进行删除节点操作时,需要使用一个temp指针来存储删除节点后一个节点的地址,否则将失去后一个节点的地址,无法进行free()操作,释放内存
5)单链表相比于顺序表,没有长度的限制,顺序表更适用于数据数量的最大值和最小值相差不大的情况(方便设定长度)
4.运行结果
Hello!
Cannot delete a
Hll!
linking
Holl!
The first node: 6487520, 6487520, 6487528
The second node: 6487504, 6487504, 6487512
--------------------------------
Process exited after 0.0464 seconds with return value 6487504
请按任意键继续. . .