#include<stdio.h>
#include<stdlib.h>
typedef struct Node* NodePtr;
typedef struct Node
{
int data;
NodePtr prev;
NodePtr next;
} Node;
void putPrev(int data, NodePtr head) {
NodePtr node = (NodePtr)malloc(sizeof(Node));
if (node==NULL)
{
printf("node==NUll");
return;
}
node->data = data;
node->next = NULL;
node->prev = NULL;
NodePtr p = head;
while (p->prev!=NULL)
{
p = p->prev;
}
node->next = p;
p->prev = node;
}
void putNext(int data, NodePtr head) {
NodePtr node = (NodePtr)malloc(sizeof(Node));
if (node == NULL)
{
printf("node==NUll");
return;
}
node->data = data;
node->next = NULL;
node->prev = NULL;
NodePtr p = head;
while (p->next != NULL)
{
p = p->next;
}
node->prev = p;
p->next = node;
}
//插入并排序
void putBySort(int data, NodePtr head) {
NodePtr node = (NodePtr)malloc(sizeof(Node));
if (node == NULL)
{
printf("node==NUll");
return;
}
node->data = data;
node->next = NULL;
node->prev = NULL;
NodePtr p = head;
if (data>p->data)
{
NodePtr pPrev = p;//保存P的前一个节点,在P=NULL的时候,P的前一个节点就代表是这个链表的最大值,如果data还大于目前最大值,则把data的节点设置为最大值
while (p!=NULL&&data > p->data) {
pPrev = p;
p = p->next;
}
if (p == NULL)
{
pPrev->next = node;
node->prev = pPrev;
}
else {
pPrev->next = node;
node->prev = pPrev;
node->next = p;
p->prev = node;
}
}
else {
NodePtr pNext = p;//保存P的后一个节点,在P=NULL的时候,P的后一个节点就代表是这个链表的最小值,如果data还小于目前最小值,则把data的节点设置为最大值
while (p != NULL&&data < p->data) {
pNext = p;
p = p->prev;
}
if (p == NULL)
{
pNext->prev = node;
node->next = pNext;
}
else {
pNext->prev = node;
node->next = pNext;
node->prev = p;
p->next = node;
}
}
}
//冒泡排序
void sort(NodePtr head) {
NodePtr p = head;
NodePtr pNext = p->next;
if (pNext == NULL)//只有一个参数
{
return;
}
if (pNext->next ==NULL && p->data > pNext->data)//只有两个参数
{
p->next = NULL;
p->prev = pNext;
pNext->next = p;
pNext->prev = NULL;
return;
}
NodePtr q;
int tmp;
for (; p->next!=NULL; p=p->next)
{
for (q=p->next; q!=NULL; q = q->next)
{
if (q->data < p->data)
{
tmp = q->data;
q->data = p->data;
p->data = tmp;
}
}
}
}
//查
NodePtr getNode(int position, NodePtr head) {
NodePtr p = head;
int i = 0;
for (; p != NULL; p = p->next) {
i++;
if (i == position)
{
break;
}
}
return p;
}
//增
void insertNode(int data, int position,NodePtr head) {
NodePtr node = (NodePtr)malloc(sizeof(Node));
node->data = data;
node->next = NULL;
node->prev = NULL;
NodePtr p = getNode(position, head);
NodePtr pNext = p->next;
NodePtr pPrev = p->prev;
pPrev->next = node;
node->prev = pPrev;
node->next = p;
p->prev = node;
}
//删除节点
void deleteNode(int position, NodePtr head) {
NodePtr p = getNode(position, head);
NodePtr pNext = p->next;
NodePtr pPrev = p->prev;
pPrev->next = pNext;
pNext->prev = pPrev;
p->next = NULL;
p->prev = NULL;
p->data = 0;
}
//改
void modifyNode(int data,int position, NodePtr head) {
NodePtr p = getNode(position, head);
p->data = data;
}
//打印链表
void printNode(NodePtr list) {
//找到最前面的节点
NodePtr nodePrev = list;
while (nodePrev->prev != NULL)
{
nodePrev = nodePrev->prev;
}
NodePtr node = nodePrev;
while (node != NULL)
{
printf("%d ", node->data);
node = node->next;
if (node!=NULL)
{
printf(",");
}
}
printf("\n");
}
void main() {
int i = 0;
NodePtr list= (NodePtr)malloc(sizeof(Node));
list->data = 10;
list->next = NULL;
list->prev = NULL;
int intArr[] = {5,1,20,15,22,11,19,50,18,40,45,30,65};
int length = sizeof(intArr) / sizeof(intArr[0]);
//插入的时候排序
/*for (i = 0; i < length; i++)
{
putBySort(intArr[i], list);
}*/
//插入数据
for (i = 0; i < length; i++)
{
putNext(intArr[i], list);
}
//首次插入数据后
printf("首次插入数据后:\n");
printNode(list);
//增
int position = 5;
int addNum = 6;
insertNode(addNum, position, list);
//添加数字后
printf("在第%d位插入数据%d后:\n",position,addNum);
printNode(list);
//删
position = 10;
deleteNode(position, list);
printf("删除第%d位数据后:\n", position);
printNode(list);
//改
position = 4;
int modifyNum = 12;
modifyNode(modifyNum, position, list);
printf("将第%d位数据改为%d后:\n", position,modifyNum);
printNode(list);
//查
position = 9;
NodePtr p = getNode(position,list);
printf("第%d位数据为%d。\n", position, p->data);
//冒泡排序
sort(list);
printf("冒泡排序后:\n");
printNode(list);
system("pause");
}