#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int len;
//定义双向链表节点
typedef struct Node {
int data;
struct Node* prev;
struct Node* next;
}Node;
//初始化一个链表节点
Node * create_node() {
Node *p = (Node *)malloc(sizeof(Node));
scanf("%d", &(p->data));
p->prev = NULL;
p->next = NULL;
return p;
}
//创建含有n个节点的双链表
Node * create_list(int n) {
Node *p, *newList, *head;
int i;
if (n >= 1) { //节点个数>=1时先生成第一个节点
newList = create_node();
head = newList;
p = newList;
}
for (i = 2; i <= n; i++) { //生成第一个节点后面的节点,建立双向链表的关系
newList = create_node();
p->next = newList;
newList->prev = p;
p = newList;
}
len = n;
if (n >= 1)
return head;
else
{
return 0;
}
}
//链表长度
int len_list() {
return len;
}
//定位到链表的任意位置
Node * pos_list(Node *head, int n) {
int i = 1;
Node *p;
if (i <= n) {
p = head;
for (i = 2; i <= n; i++) {
p = p->next;
}
}
return p;
}
//正向遍历链表
void traver_front_list(Node *head) {
if (head == NULL) {
printf("链表不存在!\n");
}
else
{
Node *p;
p = head;
while (p!=NULL)
{
printf("%d ", p->data);
p = p->next;
}
}
}
//反向遍历链表
void traver_back_list(Node *head) {
if (head == NULL) {
printf("链表不存在!\n");
}
else
{
int n = len_list();
Node *p;
p = pos_list(head,n);
while (p != NULL)
{
printf("%d ", p->data);
p = p->prev;
}
}
}
//在链表的头部插入结点
Node* push_start_list(Node *head)
{
Node *p;
p = create_node();
p->next = head;
head->prev = p;
head = p;
len++;
return head;
}
//在链表的尾部插入结点
Node* push_end_list(Node *head)
{
int n = len_list();
Node *p,*newList;
p = pos_list(head,n);
newList = create_node();
p->next = newList;
newList->prev = p;
len++;
return head;
}
//插入到任意位置之前
Node* insert_befor_list(Node *head)
{
int a, newlen;
Node *pos, *p;
printf("请输入要插入结点的位置:");
scanf("%d", &a);
printf("请输入要插入的结点的值:");
newlen = len_list();
if (a > newlen)
{
head = push_end_list(head);
}
else
{
if (a <= 1)
{
head = push_start_list(head);
}
else
{
pos = pos_list(head, a);
p = create_node();
pos->prev->next = p;
p->prev = pos->prev;
p->next = pos;
pos->prev = p;
}
}
len++;
return head;
}
//插入到任意位置之后
Node* insert_after_list(Node *head)
{
int a, newlen;
Node *pos, *p;
printf("请输入要插入结点的位置:");
scanf("%d", &a);
printf("请输入要插入的结点的值:");
newlen = len_list();
if (a >= newlen)
{
head = push_end_list(head);
}
else
{
if (a < 1)
{
head = push_start_list(head);
}
else
{
pos = pos_list(head, a);
p = create_node();
p->next = pos->next;
pos->next->prev = p;
pos->next = p;
p->prev = pos;
}
}
len++;
return head;
}
//删除头结点
Node* delect_start_list(Node *head)
{
Node *p;
p = head;
head = head->next;
head->prev = NULL;
free(p);
len--;
return(head);
}
//删除尾结点
Node* delect_end_list(Node *head)
{
Node *p, *pos;
int newlen;
newlen = len_list();
pos = pos_list(head, newlen);
p = pos;
p = p->prev;
p->next = NULL;
free(pos);
len--;
return (head);
}
//删除指定位置的节点
Node* delect_list(Node *head)
{
int newlen, i;
Node *pos;
newlen = len_list();
printf("请输入要删除结点的位置:\n");
scanf("%d", &i);
if (i <= 1)
head = delect_start_list(head);
else if (i >= newlen)
head = delect_end_list(head);
else
{
pos = pos_list(head, i);
pos->prev->next = pos->next;
pos->next->prev = pos->prev;
free(pos);
}
len--;
return(head);
}
int main()
{
//函数的声明
Node* create_node(void); //定义双向链表的节点
Node* create_list(int n); //建立含有N个结点的双向链表
int len_list(); //链表的长度
Node* pos_list(Node *head, int n); //定位到链表的任意位置
void traver_front_list(Node *head); //正向遍历一个链表
void traver_back_list(Node *head); //反向遍历一个链表
Node* push_start_list(Node *head); //在链表的头部插入结点
Node* push_end_list(Node *head); //在链表的尾部插入结点
Node* insert_befor_list(Node *head); //插入到任意位置之前
Node* insert_after_list(Node *head); //插入到任意位置之后
Node* delect_start_list(Node *head); //删除头结点
Node* delect_end_list(Node *head); //删除尾结点
Node* delect_list(Node *head); //删除指定位置的节点
Node *head;
printf("请输入要建立双向链表的长度:\n");
scanf("%d", &len);
printf("请为双向链表赋值:\n");
head = create_list(len);
printf("链表的长度为:%d\n", len = len_list());
printf("正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n请输入在链表头部插入结点的值:\n");
head = push_start_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n请输入在链表尾部插入结点的值:\n");
head = push_end_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n插入到任意位置之前:\n");
head = insert_befor_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n插入到任意位置之后:\n");
head = insert_after_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n删除头结点:\n");
head = delect_start_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n删除尾结点:\n");
head = delect_end_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n删除指定位置的结点:\n");
head = delect_list(head);
printf("链表的长度为:%d", len = len_list());
printf("\n正向遍历双向链表:\n");
traver_front_list(head);
printf("\n链表的长度为:%d", len = len_list());
printf("\n反向遍历双向链表:\n");
traver_back_list(head);
printf("\n链表的长度为:%d", len = len_list());
system("pause");
return 0;
}