📄 d_insert.java
字号:
/* =============== Program Description =============== */
/* 程序名称: d_insert.c */
/* 程序目的: 设计一个双向链表内节点插入的程序。 */
/* Written By Kuo-Yu Huang. (WANT Studio.) */
/* =================================================== */
#include <stdlib.h>
#define Max1 10
struct DList /* 节点结构声明 */
{
int Number;
struct DList *Back;
struct DList *Next;
};
typedef struct DList DNode;
typedef DNode *DLink;
int Data1[Max1] = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
/* --------------------------------------------------- */
/* 释放双向链表 */
/* --------------------------------------------------- */
void Free_DList(DLink Head)
{
DLink Pointer; /* 节点声明 */
while ( Head != NULL ) /* 当节点为NULL结束循环 */
{
Pointer = Head;
Head = Head->Next; /* 往下一个节点 */
free(Pointer);
}
}
/* --------------------------------------------------- */
/* 插入节点至双向链表内 */
/* --------------------------------------------------- */
DLink Insert_DList(DLink Head,DLink New,int Key)
{
DLink Pointer; /* 节点声明 */
Pointer = Head; /* Pointer指针设为首节点 */
while ( 1 )
{
/* 插入在链表尾端 */
if ( Pointer->Number == Key )
{
New->Next = Pointer->Next;
New->Back = Pointer;
Pointer->Next = New;
/* 插入在链表中间 */
if ( New->Next != NULL )
New->Next->Back = New;
break;
}
/* 找不到数据,插入在链表首节点前 */
if ( Pointer->Number != Key && Pointer->Next == Head )
{
New->Next = Pointer;
Pointer->Back = New;
Head = New;
break;
}
Pointer = Pointer->Next; /* 往下一个节点 */
}
return Head;
}
/* --------------------------------------------------- */
/* 输出双向链表数据 */
/* --------------------------------------------------- */
void Print_DList(DLink Head)
{
DLink Pointer; /* 节点声明 */
Pointer = Head; /* Pointer指针设为首节点 */
printf("Input Data : \n");
while ( Pointer != NULL ) /* 当节点为NULL结束循环 */
{
printf("[%d]",Pointer->Number);
Pointer = Pointer->Next; /* 往下一个节点 */
}
printf("\n");
}
/* --------------------------------------------------- */
/* 建立双向链表 */
/* --------------------------------------------------- */
DLink Create_DList(DLink Head,int *Data,int Max)
{
DLink New; /* 节点声明 */
DLink Pointer; /* 节点声明 */
int i;
Head = (DLink) malloc(sizeof(DNode)); /* 存储空间配置 */
if ( Head == NULL ) /* 存储空间配置失败 */
printf("Memory allocate Failure!!\n");
else
{
Head->Number = Data[0]; /* 定义首节点数据编号 */
Head->Back = NULL;
Head->Next = NULL;
Pointer = Head; /* Pointer指针设为首节点 */
for ( i=1;i<Max;i++)
{
/* 存储空间配置 */
New = (DLink) malloc(sizeof(DNode));
New->Number = Data[i];
New->Back = NULL;
New->Next = NULL;
/* 将新节点串连在原链表尾端 */
Pointer->Next = New;
/* 新节点的前一个节点为原链表尾端 */
New->Back = Pointer;
/* 链表尾端节点为新节点 */
Pointer = New;
}
}
return Head;
}
/* --------------------------------------------------- */
/* 主程序 */
/* --------------------------------------------------- */
void main ()
{
DLink Head; /* 节点声明 */
DLink New;
int Key;
Head = Create_DList(Head,Data1,Max1); /* 调用建立双向链表 */
if ( Head != NULL )
{
Print_DList(Head); /* 调用输出双向链表数据 */
while ( 1 )
{
printf("Input 0 to EXIT\n"); /* 数据输入提示 */
New = (DLink) malloc(sizeof(DNode)); /* 存储空间配置 */
printf("Please input the data : ");
scanf("%d",&New->Number);
if ( New->Number == 0 ) /* 输入0时结束循环 */
break;
printf("Please input the data number for Insert : ");
scanf("%d",&Key);
Head = Insert_DList(Head,New,Key); /* 调用插入节点 */
Print_DList(Head); /* 输出链表数据 */
}
Free_DList(Head);
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -