目录
先附上老师的代码
#include <stdio.h>
#include <malloc.h>
/**
* Double linked list of integers. The key is char.
*/
typedef struct DoubleLinkedNode{
char data;
struct DoubleLinkedNode *previous;
struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
DLNodePtr initLinkList(){
DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
tempHeader->data = '\0';
tempHeader->previous = NULL;
tempHeader->next = NULL;
return tempHeader;
}// Of initLinkList
/**
* Print the list.
* @param paraHeader The header of the list.
*/
void printList(DLNodePtr paraHeader){
DLNodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}// Of while
printf("\r\n");
}// Of printList
/**
* 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(DLNodePtr paraHeader, char paraChar, int paraPosition){
DLNodePtr p, q, r;
// Step 1. Search to the position.
p = paraHeader;
for (int i = 0; i < paraPosition; i ++) {
p = p->next;
if (p == NULL) {
printf("The position %d is beyond the scope of the list.", paraPosition);
return;
}// Of if
} // Of for i
// Step 2. Construct a new node.
q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
q->data = paraChar;
// Step 3. Now link.
r = p->next;
q->next = p->next;
q->previous = p;
p->next = q;
if (r != NULL) {
r->previous = q;
}// Of if
}// Of insertElement
/**
* Delete an element from the list.
* @param paraHeader The header of the list.
* @param paraChar The given char.
*/
void deleteElement(DLNodePtr paraHeader, char paraChar){
DLNodePtr p, q, r;
p = paraHeader;
// Step 1. Locate.
while ((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}// Of while
// Step 2. Error check.
if (p->next == NULL) {
printf("The char '%c' does not exist.\r\n", paraChar);
return;
}// Of if
// Step 3. Change links.
q = p->next;
r = q->next;
p->next = r;
if (r != NULL) {
r->previous = p;
}// Of if
// Step 4. Free the space.
free(q);
}// Of deleteElement
/**
* Unit test.
*/
void insertDeleteTest(){
// Step 1. Initialize an empty list.
DLNodePtr tempList = initLinkList();
printList(tempList);
// Step 2. Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
insertElement(tempList, '!', 5);
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
/**
* Address test: beyond the book.
*/
void basicAddressTest(){
DLNode 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
/**
* The entrance.
*/
int main(){
insertDeleteTest();
basicAddressTest();
}// Of main
那么单链表与双链表的区别在哪里呢?
单链表只有一个指向下一结点的指针,也就是只能next
双链表除了有一个指向下一结点的指针外,还有一个指向前一结点的指针,可以通过prev()快速找到前一节点,顾名思义,单链表只能单向读取
如果我们对单链表的代码进行修改修改成双链表,要怎么修改呢?
假设现在有个这样的链表
1 --> 2 --> 3
链表逆置之后变成:
1 <-- 2 <-- 3
下面的内容中 next表示结构体中下一个节点的地址
下面是步骤:
1、保存上一节点的地址LAST(一开始的时候因为没有上一节点,所以L = NULL)
2、保存当前节点的地址NOW(一开始的时候是1的地址)
3、保存下一节点的地址NEXT(一开始的时候是1的下一节点的地址,即2的地址)
4、NEXT = NOW->next(保存下一节点的地址到NEXT)
5、NOW->next = LAST(断开 1 --> 2 的箭头,此时变成 1 2 --> 3)
6、LAST = NOW (把当前节点地址保存到LAST)
7、NOW = NEXT (移动到下一个节点)
8、NEXT = NOW->next(记录下一个节点的地址到NEXT)
9、NOW->next = LAST(断开 2 --> 3 的箭头,并建立 2 --> 1,因为LAST存的是1的地址)
10、LAST = NOW(把当前节点地址保存到LAST)
11、NOW = NEXT(移动到下一节点)
12、NEXT = NOW->next(记录下一个节点的地址到NEXT)
13、NOW->next = LAST(建立 3 --> 2,因为LAST存的是2的地址)
14、LAST = NOW(把当前节点地址保存到LAST)
发现没有,步骤其实是重复的!
重复的步骤就可以建立循环来解决
那循环条件是什么呢?
想一下什么时候循环:当前节点滞后还有节点的时候就循环
所以循环条件就是当前节点的next指针不为NULL,不为NULL的时候,就表示后面还有节点!
以上是单链表的逆置
转换成双向链表
其实差不多,也是记录NOW、NEXT、LAST三个值
双向链表与单向链表的区别就只是多了一个指向前一个节点的指针(假设是last)。
在遍历整个链表的时候只要 NOW->last = LAST 加一句这个就好了
下面附上我的代码
// 结点的查找
int locateElement(DLNode *head, char targetElement)
{
DLNode *t = head;
int i = 1;
while(t){
if(t->data==targetElement)return i;
i++;
t=t->next;
}
return -1;
}
静态链表
老师的代码先放上
#include <stdio.h>
#include <malloc.h>
#define DEFAULT_SIZE 5
typedef struct StaticLinkedNode{
char data;
int next;
} *NodePtr;
typedef struct StaticLinkedList{
NodePtr nodes;
int* used;
} *ListPtr;
/**
* Initialize the list with a header.
* @return The pointer to the header.
*/
ListPtr initLinkedList(){
// The pointer to the whole list space.
ListPtr tempPtr = (ListPtr)malloc(sizeof(StaticLinkedList));
// Allocate total space.
tempPtr->nodes = (NodePtr)malloc(sizeof(struct StaticLinkedNode) * DEFAULT_SIZE);
tempPtr->used = (int*)malloc(sizeof(int) * DEFAULT_SIZE);
// The first node is the header.
tempPtr->nodes[0].data = '\0';
tempPtr->nodes[0].next = -1;
// Only the first node is used.
tempPtr->used[0] = 1;
for (int i = 1; i < DEFAULT_SIZE; i ++){
tempPtr->used[i] = 0;
}// Of for i
return tempPtr;
}// Of initLinkedList
/**
* Print the list.
* @param paraListPtr The pointer to the list.
*/
void printList(ListPtr paraListPtr){
int p = 0;
while (p != -1) {
printf("%c", paraListPtr->nodes[p].data);
p = paraListPtr->nodes[p].next;
}// Of while
printf("\r\n");
}// Of printList
/**
* Insert an element to the given position.
* @param paraListPtr The position of the list.
* @param paraChar The given char.
* @param paraPosition The given position.
*/
void insertElement(ListPtr paraListPtr, char paraChar, int paraPosition){
int p, q, i;
// Step 1. Search to the position.
p = 0;
for (i = 0; i < paraPosition; i ++) {
p = paraListPtr->nodes[p].next;
if (p == -1) {
printf("The position %d is beyond the scope of the list.\r\n", paraPosition);
return;
}// Of if
} // Of for i
// Step 2. Construct a new node.
for (i = 1; i < DEFAULT_SIZE; i ++){
if (paraListPtr->used[i] == 0){
// This is identical to malloc.
printf("Space at %d allocated.\r\n", i);
paraListPtr->used[i] = 1;
q = i;
break;
}// Of if
}// Of for i
if (i == DEFAULT_SIZE){
printf("No space.\r\n");
return;
}// Of if
paraListPtr->nodes[q].data = paraChar;
// Step 3. Now link.
printf("linking\r\n");
paraListPtr->nodes[q].next = paraListPtr->nodes[p].next;
paraListPtr->nodes[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(ListPtr paraListPtr, char paraChar){
int p, q;
p = 0;
while ((paraListPtr->nodes[p].next != -1) && (paraListPtr->nodes[paraListPtr->nodes[p].next].data != paraChar)){
p = paraListPtr->nodes[p].next;
}// Of while
if (paraListPtr->nodes[p].next == -1) {
printf("Cannot delete %c\r\n", paraChar);
return;
}// Of if
q = paraListPtr->nodes[p].next;
paraListPtr->nodes[p].next = paraListPtr->nodes[paraListPtr->nodes[p].next].next;
// This statement is identical to free(q)
paraListPtr->used[q] = 0;
}// Of deleteElement
/**
* Unit test.
*/
void appendInsertDeleteTest(){
// Step 1. Initialize an empty list.
ListPtr tempList = initLinkedList();
printList(tempList);
// Step 2. Add some characters.
insertElement(tempList, 'H', 0);
insertElement(tempList, 'e', 1);
insertElement(tempList, 'l', 2);
insertElement(tempList, 'l', 3);
insertElement(tempList, 'o', 4);
printList(tempList);
// Step 3. Delete some characters (the first occurrence).
printf("Deleting 'e'.\r\n");
deleteElement(tempList, 'e');
printf("Deleting 'a'.\r\n");
deleteElement(tempList, 'a');
printf("Deleting 'o'.\r\n");
deleteElement(tempList, 'o');
printList(tempList);
insertElement(tempList, 'x', 1);
printList(tempList);
}// Of appendInsertDeleteTest
/**
* The entrance.
*/
int
单链表跟双链表的区别在哪里呢?(转载)
1、静态链表是用类似于数组方法实现的,是顺序的存储结构,在物理地址上是连续的,而且需要预先分配地址空间大小。所以静态链表的初始长度一般是固定的,在做插入和删除操作时不需要移动元素,仅需修改指针。
2、动态链表是用内存申请函数(malloc/new)动态申请内存的,所以在链表的长度上没有限制。动态链表因为是动态申请内存的,所以每个节点的物理地址不连续,要通过指针来顺序访问()
原文链接:https://blog.csdn.net/zhengqijun_/article/details/78192888
附上我的代码
#include <stdio.h>
#define maxSize 6
typedef struct {
int data;
int cur;
}component;
//将结构体数组中所有分量链接到备用链表中
void reserveArr(component *array);
//初始化静态链表
int initArr(component *array);
//输出函数
void displayArr(component * array, int body);
//从备用链表上摘下空闲节点的函数
int mallocArr(component * array);
int main() {
component array[maxSize];
int body = initArr(array);
printf("静态链表为:\n");
displayArr(array, body);
return 0;
}
//创建备用链表
void reserveArr(component *array) {
int i = 0;
for (i = 0; i < maxSize; i++) {
array[i].cur = i + 1;//将每个数组分量链接到一起
array[i].data = 0;
}
array[maxSize - 1].cur = 0;//链表最后一个结点的游标值为0
}
//提取分配空间
int mallocArr(component * array) {
//若备用链表非空,则返回分配的结点下标,否则返回 0(当分配最后一个结点时,该结点的游标值为 0)
int i = array[0].cur;
if (array[0].cur) {
array[0].cur = array[i].cur;
}
return i;
}
//初始化静态链表
int initArr(component *array) {
int tempBody = 0, body = 0;
int i = 0;
reserveArr(array);
body = mallocArr(array);
//建立首元结点
array[body].data = 1;
array[body].cur = 0;
//声明一个变量,把它当指针使,指向链表的最后的一个结点,当前和首元结点重合
tempBody = body;
for (i = 2; i < 4; i++) {
int j = mallocArr(array); //从备用链表中拿出空闲的分量
array[j].data = i; //初始化新得到的空间结点
array[tempBody].cur = j; //将新得到的结点链接到数据链表的尾部
tempBody = j; //将指向链表最后一个结点的指针后移
}
array[tempBody].cur = 0;//新的链表最后一个结点的指针设置为0
return body;
}
void displayArr(component * array, int body) {
int tempBody = body;//tempBody准备做遍历使用
while (array[tempBody].cur) {
printf("%d,%d\n", array[tempBody].data, array[tempBody].cur);
tempBody = array[tempBody].cur;
}
printf("%d,%d\n", array[tempBody].data, array[tempBody].cur);
}