双向链表
1.创建一个双向链表
老师的(没有循环)
//定义一个双向链表
typedef struct DoubleLinkedNode
{
char data;
struct DoubleLinkedNode *previous;
struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;
//双向链表的初始化
DLNodePtr initLinkList()
{
DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
tempHeader->previous = NULL;
return tempHeader;
}
自写的(循环)
struct doubleList
{
int data;
struct doubleList* front;
struct doubleList* tail;
};
//创建表头表示链表,创建表过程
struct doubleList* createList()
{
//指针->变量
struct doubleList* headNode = (struct doubleList*)malloc(sizeof(struct doubleList));
//变量基本原则:使用前要初始化
//有表头:数据差异化当作表头
headNode->front = headNode->tail = headNode;
headNode->data = '\0';//可有可无
return headNode;
}
2.创建新的结点
自写的
//为插入做准备:创建插入的结点
struct doubleList* createNode(int data)
{
struct doubleList* newNode = (struct doubleList*)malloc(sizeof(struct doubleList));
newNode->data = data;
newNode->front = newNode->tail = NULL;
return newNode;
}
3.打印
老师的
//打印链表
void printList(DLNodePtr paraHeader)
{
DLNodePtr p = paraHeader->next;
while (p != NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\r\n");
}
自写的
//后指针打印链表
void printListBytail(struct doubleList* headNode)
{
struct doubleList* pMove = headNode->tail;
while(pMove != headNode)
{
printf("%d ",pMove->data);
pMove = pMove->tail;
}
printf("\n");
}
//前指针打印链表
void printListByfront(struct doubleList* headNode)
{
struct doubleList* pMove = headNode->front;
while(pMove != headNode)
{
printf("%d ",pMove->data);
pMove = pMove->front;
}
printf("\n");
}
4.插入元素
老师的
//指定位置插入元素
void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition)
{
DLNodePtr p, q, r;
//第一步:找到位置
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;
}
}
//第二步:创建一个新的结点
q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
q->data = paraChar;
//第三步:连接
r = p->next;
q->next = p->next;
q->previous = p;
p->next = q;
if (r != NULL)
{
r->previous = q;
}
}
自写的
//表尾插入
void insertNodeBytail(struct doubleList* headNode,int data)
{
struct doubleList* newNode = createNode(data);
//找到lastNode
struct doubleList* lastNode = headNode;
while (lastNode->tail != headNode)
{
lastNode = lastNode->tail;
}
headNode->front = newNode;
newNode->tail = headNode;
lastNode->tail = newNode;
newNode->front = lastNode;
}
//表头插入
void insertNodeByhead(struct doubleList* headNode,int data)
{
struct doubleList* newNode = createNode(data);
newNode->tail = headNode->tail;
newNode->front = headNode;
headNode->tail->front = newNode;
headNode->tail = newNode;
}
//指定值后面插入
void insertNodeBydata(struct doubleList* headNode,int data,int paradata)
{
struct doubleList* newNode = createNode(data);
struct doubleList* p = headNode->tail;
while(p->data != paradata)
{
// if(p->tail == headNode)
// {
// printf("未能找到该值,不能插入\n");
// return;
// }
// p = p->tail;
p = p->tail;
if(p == headNode)
{
printf("未能找到该值,不能插入\n");
return;
}
}
newNode->tail = p->tail;
p->tail->front = newNode;
newNode->front = p;
p->tail = newNode;
}
5.删除元素
老师的
//删除指定值
void deleteElement(DLNodePtr paraHeader, char paraChar)
{
DLNodePtr p, q, r;
p = paraHeader;
//第一步:定位元素值
while (p->next != NULL && p->next->data != paraChar)
{
p = p->next;
}
//第二步:错误检查
if (p->next == NULL)
{
printf("The cahr %c does not exist.\r\n", paraChar);
return;
}
//第三步:删除
q = p->next;
r = q->next;
p->next = r;
if (r != NULL)
{
r->previous = p;
}
//第四步:释放空间
free(q);
}
自写的
//指定值删除
void deleteNodeBydata(struct doubleList* headNode,int posData)
{
struct doubleList* posNode = headNode->tail;
struct doubleList* posNodefront = headNode;
while(posNode->data != posData)
{
if(posNode->tail == headNode)
{
printf("没有找到该值,无法删除。\n");
return;
}
posNodefront = posNode;
posNode = posNode->tail;
}
posNodefront->tail = posNode->tail;
posNode->tail->front = posNodefront;
free(posNode);
}
6.全部代码
老师的
#include <stdio.h>
#include <malloc.h>
//定义一个双向链表
typedef struct DoubleLinkedNode
{
char data;
struct DoubleLinkedNode *previous;
struct DoubleLinkedNode *next;
} DLNode, *DLNodePtr;
//双向链表的初始化
DLNodePtr initLinkList()
{
DLNodePtr tempHeader = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
tempHeader->previous = NULL;
return tempHeader;
}
//打印链表
void printList(DLNodePtr paraHeader)
{
DLNodePtr p = paraHeader->next;
while (p != NULL)
{
printf("%c", p->data);
p = p->next;
}
printf("\r\n");
}
//指定位置插入元素
void insertElement(DLNodePtr paraHeader, char paraChar, int paraPosition)
{
DLNodePtr p, q, r;
//第一步:找到位置
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;
}
}
//第二步:创建一个新的结点
q = (DLNodePtr)malloc(sizeof(struct DoubleLinkedNode));
q->data = paraChar;
//第三步:连接
r = p->next;
q->next = p->next;
q->previous = p;
p->next = q;
if (r != NULL)
{
r->previous = q;
}
}
//删除指定值
void deleteElement(DLNodePtr paraHeader, char paraChar)
{
DLNodePtr p, q, r;
p = paraHeader;
//第一步:定位元素值
while (p->next != NULL && p->next->data != paraChar)
{
p = p->next;
}
//第二步:错误检查
if (p->next == NULL)
{
printf("The cahr %c does not exist.\r\n", paraChar);
return;
}
//第三步:删除
q = p->next;
r = q->next;
p->next = r;
if (r != NULL)
{
r->previous = p;
}
//第四步:释放空间
free(q);
}
//测试
void insertDeleteTest()
{
//第一步:创建一个空的链表
DLNodePtr tempList = initLinkList();
printList(tempList);
//第二步:添加值
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);
//第三步:删除
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printList(tempList);
//第四步:插入
insertElement(tempList, 'o', 1);
printList(tempList);
}
//空间测试
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;
}
int main()
{
insertDeleteTest();
basicAddressTest();
}
自写的
#include<stdio.h>
#include<stdlib.h>
struct doubleList
{
int data;
struct doubleList* front;
struct doubleList* tail;
};
//创建表头表示链表,创建表过程
struct doubleList* createList()
{
//指针->变量
struct doubleList* headNode = (struct doubleList*)malloc(sizeof(struct doubleList));
//变量基本原则:使用前要初始化
//有表头:数据差异化当作表头
headNode->front = headNode->tail = headNode;
headNode->data = '\0';//可有可无
return headNode;
}
//为插入做准备:创建插入的结点
struct doubleList* createNode(int data)
{
struct doubleList* newNode = (struct doubleList*)malloc(sizeof(struct doubleList));
newNode->data = data;
newNode->front = newNode->tail = NULL;
return newNode;
}
//表尾插入
void insertNodeBytail(struct doubleList* headNode,int data)
{
struct doubleList* newNode = createNode(data);
//找到lastNode
struct doubleList* lastNode = headNode;
while (lastNode->tail != headNode)
{
lastNode = lastNode->tail;
}
headNode->front = newNode;
newNode->tail = headNode;
lastNode->tail = newNode;
newNode->front = lastNode;
}
//表头插入
void insertNodeByhead(struct doubleList* headNode,int data)
{
struct doubleList* newNode = createNode(data);
newNode->tail = headNode->tail;
newNode->front = headNode;
headNode->tail->front = newNode;
headNode->tail = newNode;
}
//指定值后面插入
void insertNodeBydata(struct doubleList* headNode,int data,int paradata)
{
struct doubleList* newNode = createNode(data);
struct doubleList* p = headNode->tail;
while(p->data != paradata)
{
// if(p->tail == headNode)
// {
// printf("未能找到该值,不能插入\n");
// return;
// }
// p = p->tail;
p = p->tail;
if(p == headNode)
{
printf("未能找到该值,不能插入\n");
return;
}
}
newNode->tail = p->tail;
p->tail->front = newNode;
newNode->front = p;
p->tail = newNode;
}
//指定值删除
void deleteNodeBydata(struct doubleList* headNode,int posData)
{
struct doubleList* posNode = headNode->tail;
struct doubleList* posNodefront = headNode;
while(posNode->data != posData)
{
if(posNode->tail == headNode)
{
printf("没有找到该值,无法删除。\n");
return;
}
posNodefront = posNode;
posNode = posNode->tail;
}
posNodefront->tail = posNode->tail;
posNode->tail->front = posNodefront;
free(posNode);
}
//后指针打印链表
void printListBytail(struct doubleList* headNode)
{
struct doubleList* pMove = headNode->tail;
while(pMove != headNode)
{
printf("%d ",pMove->data);
pMove = pMove->tail;
}
printf("\n");
}
//前指针打印链表
void printListByfront(struct doubleList* headNode)
{
struct doubleList* pMove = headNode->front;
while(pMove != headNode)
{
printf("%d ",pMove->data);
pMove = pMove->front;
}
printf("\n");
}
void Test()
{
struct doubleList* List = createList();
insertNodeByhead(List,1);
insertNodeByhead(List,2);
insertNodeByhead(List,3);
insertNodeByhead(List,4);
insertNodeByhead(List,5);
printListByfront(List);
printListBytail(List);
insertNodeBytail(List,6);
printListBytail(List);
deleteNodeBydata(List,2);
printListBytail(List);
deleteNodeBydata(List,6);
printListBytail(List);
insertNodeBydata(List,7,1);
printListBytail(List);
}
int main()
{
Test();
}
7.测试结果
老师的
Hello!
The cahr a does not exist.
Hll!
Holl!
The first node: 6421968 ,6421968, 6421984
The second node: 6421936 ,6421936, 6421952
自写的
1 2 3 4 5
5 4 3 2 1
5 4 3 2 1 6
5 4 3 1 6
5 4 3 1
5 4 3 1 7