单链表
#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;
}// Of initLinkList
/**
* Print the list.
* @param paraHearder The header of the list.
*/
void printList(NodePtr paraHeader) {
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}//Of while
printf("\r\n");
}//Of printList
/**
* Add an element to the tail.
* @param paraHeader The header of the list.
* @param paraChar The given char.
*/
void appendElement(NodePtr paraHeader, char paraChar) {//append附加
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;
}//Of while
//Step 3. Now add/link.
p->next = q;
}// Of appendElement
/**
* 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;
int i;
//Step 1. Search to the position.
p =paraHeader;
for(i = 0; i < paraPosition; i ++) {
p = p->next;
if(p == NULL) {
printf("The position %d is beyond the scope of the list.", paraPosition);//scope 范围
return;
}//Of if
}//Of for i
// Step 2. Construct a new code.
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
// Step 3. Now link.
printf("linking\r\n");
q->next = p->next;
p->next = q;
}//Of insertElement
/**
* 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;
while((p->next != NULL) && (p->next->data != paraChar)) {
p = p->next;
}//Of while
if(p->next == NULL) {
printf("Cannot delete %c\r\n", paraChar);
return 0;
}//Of if
q = p->next;
p->next = p->next->next;
free(q);
}//Of deleteElement
/**
* Unit test.
*/
void appendInsertDeleteTest() {
//Step 1. Initialize an empty list.
LinkList tempList = initLinkList();
printList(tempList);
//Step 2. Add some characters.
appendElement(tempList, 'H');
appendElement(tempList, 'e');
appendElement(tempList, 'l');
appendElement(tempList, 'l');
appendElement(tempList, 'o');
appendElement(tempList, '!');
printList(tempList);
//Step 3. Delete some characters (the first occurrence).
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 appendInsertDeleteTest
/**
* The entrance.
*/
int main() {
appendInsertDeleteTest();
}//Of main
运行结果如图:
由于顺序表的插入删除操作需要移动大量的元素,影响了效率,因此引入了线性表的链式储存–单链表。单链表是线性表的链式存取结构,每个节点由元素(存储数据的存储单元)+指针(连接每个节点的地址数据)构成。它不要求逻辑上相邻的两个元素在物理位置上也相邻,因此不需要连续的存储空间。单链表在运行过程中,遍历是十分重要的。它每一次对元素的删除插入,都需要从头节点开始依次往后推,直到找到目标位置。
单链表的掌握非常必要,要通过画图来辅助理解,才能更好的深入的了解链表。通过图像理清自己的思路,以免犯错。
观察地址分配
#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;
/**
* Address test: beyond the book.
*/
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;
}//Of basicAddressTest
int main() {
basicAddressTest();
return 0;
}
运行结果:
通过了解地址的分配的具体情况,更利于了解操作系统的机制,让自己对数据结构的认识也更加深入,有利于后期学习。