原博文:https://www.jb51.net/article/92979.htm
原博文:https://www.cnblogs.com/hughdong/p/6785391.html
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
双向链表
双向链表也叫双链表,是链表的一种,它的每个数据结点中都有两个指针,分别指向直接后继和直接前驱。所以,从双向链表中的任意一个结点开始,都可以很方便地访问它的前驱结点和后继结点。一般我们都构造双向循环链表。
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
和单向链表相比有以下优势:
插入删除不需要移动元素外,可以原地插入删除
可以双向遍历
- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -
初始化+尾插法图示:
//head始终指向头结点,p指向尾节点,方便后续算法使用
删除单个图示
例子1:
完成双向链表的插入、删除以及查找,将学生管理系统使用的数组,以双向链表的方式实现,能够支持无限制的学生人数的增删改查以及保存。
#include <stdio.h>
#include <string.h>
#include <stdarg.h>
#include <stdlib.h>
typedef struct Student{
char name[20];
int score;
char phoneNum[14];
} str_student;
typedef struct Node{
str_student data;
struct Node *prior; //指向前驱结点
struct Node *next; //指向后继结点
}Node, *DLinkList;
// 初始化一个学生链表
DLinkList initDouLinkList()
{
Node *L,*p,*r;
char name[20];
char phone[14];
int score;
L = (Node *)malloc(sizeof(Node));
L->next = NULL;
r = L;
r->next = NULL;
while(1)
{
p = (Node *)malloc(sizeof(Node));
printf("input name is out exit,input student name:\n");
scanf("%s",name);
if (strcmp(name,"out")==0)
{
break;
}
strcpy(p->data.name, name);
printf("input student score:");
scanf("%d",&score);
p->data.score = score;
printf("input student phone:");
scanf("%s",phone);
strcpy(p->data.phoneNum, phone);
p->next = r->next;
r->next = p;
r = p;
}
r->next = NULL;
return L;
}
//添加学生信息
DLinkList insertDouLinkListStuent(DLinkList L,int i,char *name, int score,char *phonenum)
{
DLinkList p,s;
p = L->next;
int tempi;
for(tempi = 1;tempi < i-1; tempi++)
p = p->next;
s = (Node *)malloc(sizeof(Node));
s->data.score = score;
strcpy(s->data.name,name);
strcpy(s->data.phoneNum,phonenum);
s->next = p->next;
p->next->prior = s;
s->prior = p;
p->next = s;
return L;
}
// 查找学生信息
int findDouLinkListStudent(DLinkList L,char *name)
{
DLinkList p;
p = L->next;
int i = 1;
while(p != NULL && (strcmp(p->data.name, name)!=0))
{
++i;
p = p->next;
}
if(p == NULL)
return 0;
else return i;
}
// 移除一个学生
DLinkList removeDouLinkListStudent(DLinkList L,char *name)
{
int tempi = 1;
DLinkList p;
p = L->next;
int i =findDouLinkListStudent(L,name);
while((tempi++) != i && p != NULL)
{
p = p->next;
}
if(p == NULL)
printf("no list \n");
else if(p->next == NULL)
{
p->prior->next = NULL;
free(p);
}
else
{
p->prior->next = p->next;
p->next->prior = p->prior;
free(p);
}
return L;
}
// 铺助打印信息
void printfInfo(DLinkList L)
{
DLinkList p;
p = L->next;
while (p!=NULL)
{
printf("student name %s\n",p->data.name);
printf("student name %d\n",p->data.score);
printf("student name %s\n",p->data.phoneNum);
p=p->next;
}
}
void main ()
{
char name2[20]="hanmeimei";
char phone2[14]="13612345678";
DLinkList L =initDouLinkList();
// 2.1 初始化学生双向链表数据
insertDouLinkListStuent(L,1,name2,99,phone2);
printfInfo(L);
// 2.2 查找学生zhangsan
findDouLinkListStudent(L,'zhangsan');
printfInfo(L);
// 2.3 删除学生zhangsan
removeDouLinkListStudent(L,'zhangsan');
printfInfo(L);
// 2.4 添加学生zengteng
insertDouLinkListStuent(L,9,'zengteng',89,'13643345667');
printfInfo(L);
}
例子2:运用二级指针
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct Node pNode;
struct Node
{
int data;
pNode *prev, *next;
};
/* 初始化链表,尾插法 */
pNode *InitList(pNode **head, int n)
{
pNode *p, *s;
(*head) = (pNode *)malloc(sizeof(pNode));
if ((*head) == NULL)
exit(0);
(*head)->next = NULL;//head的prev和next均指向NULL
(*head)->prev = NULL;
p = (*head);//p指向head
int i;
for (i = 0; i < n; ++i)
{
s = (pNode *)malloc(sizeof(pNode));
if (s == NULL)
exit(0);
printf("Input the value of the %dth node:", i + 1);
scanf("%d", &s->data);
s->next = NULL;
p->next = s;
s->prev = p;
p = s;//p指向尾节点
}
return p;
}
/* 遍历打印 */
void PrintList(pNode *head)
{
pNode *p;
p = head->next;
if (head->next == NULL)
printf("the list is empty\n");
while(p != NULL)
{
printf("%d ", p->data);
p = p->next;
}
printf("\n");
}
/* 清空链表 */
void DeleteList(pNode **head)
{
pNode *p;
while((*head)->next != NULL)
{
p = (*head);
p->next->prev = NULL;
(*head) = p->next;
free(p);
}
}
/* 查找链表内的某个值 */
int SearchList(pNode *head)
{
int number;
printf("Values are about to be deleted:");
scanf("%d", &number);
pNode *p;
p = head->next;
while(p != NULL)
{
if (p->data == number)
{
return number;
}
p = p->next;
}
return 0;
}
/* 删除链表中某个元素,令p的前驱节点和后驱节点相互指向即可,如果p是尾节点则直接将前驱节点指向NULL*/
void DelNumqList(pNode **head, int n)
{
int i;
pNode *p;
p = (*head)->next;
for (i = 1; i < n; ++i)
p = p->next;
if(p->next == NULL)
{
p->prev->next = NULL;
free(p);
}
else
{
p->next->prev = p->prev;
p->prev->next = p->next;
free(p);
}
}
int main(int argc, char const *argv[])
{
int n, element, flag;
pNode *head, *last;
/***************************************************************/
printf("Please input the size of the list:");
scanf("%d", &n);
last = InitList(&head, n);//初始化链表并赋值,返回尾节点last
printf("%d %d \n", head->next->data, last->data); //打印为第一个元素和最后一个元素
PrintList(head);
/***************************************************************/
flag = SearchList(head); //搜索某个值并删除节点
if (flag > 0 && flag <= n)
{
DelNumqList(&head, flag);
PrintList(head);
}
else
printf("Element does not exist, cannot be deleted\n");
/***************************************************************/
DeleteList(&head);//清空列表
PrintList(head);
return 0;
}