C语言双向链表操作
#include <stdio.h>
#include <stdlib.h>
#define TRUE 1;
#define FALSE 0;
typedef struct DuLNode{
struct DuLNode *prior, *next;
int data;
}DuLNode, *DuLinklist;
void CreateTailList(DuLinklist *L, int n){
DuLinklist p, r;
(*L) = (DuLNode *)malloc(sizeof(DuLNode));
(*L)->next = NULL;
(*L)->prior = NULL;
(*L)->data = n;
r = (*L);
for(int i = 0; i < n; i ++){
p = (DuLNode *)malloc(sizeof(DuLNode));
p->data = i;
r->next = p;
p->prior = r;
r = p;
}
r->next = NULL;
}
void CreteHeadList(DuLinklist *L, int n){
DuLinklist p, r;
(*L) = (DuLNode *)malloc(sizeof(DuLNode));
(*L)->next = NULL;
(*L)->prior = NULL;
(*L)->data = n;
r = (*L);
int i = 0;
for(i = 0; i < n; i ++){
p = (DuLNode *)malloc(sizeof(DuLNode));
p->data = i;
if(r->next == NULL){
r->next = p;
p->prior = r;
p->next = NULL;
}
else{
p->next = r->next;
r->next->prior = p;
r->next = p;
p->prior = r;
}
}
}
DuLinklist GetLast(DuLinklist *L){
DuLinklist p;
p = (*L);
while(p->next != NULL){
p = p->next;
}
return p;
}
void InsertElem(DuLinklist *L, int i, int e){
if(i <= (*L)->data){
DuLinklist p, r;
r = (*L);
p = (DuLNode *)malloc(sizeof(DuLNode));
p->data = e;
int j = 0;
for(j = 0; j < i; j ++){
r = r->next;
}
if(r->next != NULL){
p->next = r->next;
r->next->prior = p;
r->next = p;
p->prior = r;
}
else{
r->next = p;
p->prior = r;
p->next = NULL;
}
}
else{
printf("插入下标不合理,请重新输入\n");
}
}
void InsertTailNode(DuLinklist *L, int *dat, int num)
{
int i;
DuLinklist p, r;
for(i=0; i<num; i++)
{
r = GetLast(L);
p = (DuLNode *)malloc(sizeof(DuLNode));
p->data = dat[i];
r->next = p;
p->prior = r;
p->next = NULL;
}
}
void InsertHeadNode(DuLinklist *L, int *dat, int num)
{
int i;
DuLinklist p, r;
r = (*L);
for(i=0; i<num; i++)
{
p = (DuLNode *)malloc(sizeof(DuLNode));
p->data = dat[i];
p->next = r->next;
p->prior = r;
r->next = p;
r = r->next;
}
}
int GetLen(DuLinklist *L)
{
DuLinklist p;
int len = 0;
p = (*L);
while(p->next != NULL)
{
len++;
p = p->next;
}
return len;
}
void DeleteElem(DuLinklist *L, int i){
DuLinklist p, r;
if(i < GetLen(L) && i >= 0){
p = (*L);
for(int j = 0; j < i; j ++){
p = p->next;
}
if(p->next->next == NULL){
r = p->next;
p->next = NULL;
free(r);
}
else{
r = p->next;
p->next = r->next;
r->next->prior = p;
free(r);
}
}
else{
printf("下标错误\n");
}
}
void DeleteNumsNode(DuLinklist *L, int num){
if(num > 0 && num < GetLen(L)){
for(int j = 0; j < num; j++){
DeleteElem(L,0);
}
}
else{
printf("删除节点数目超出实际节点数目\n");
}
}
int GetElem(DuLinklist *L, int i){
int elem;
if(i >= 0 && i <= GetLen(L)){
DuLinklist p;
p = (*L);
for(int j = 0; j < i; j ++){
p = p->next;
}
elem = p->next->data;
printf("\n第%d个元素:%d\n",i,elem);
return elem;
}
else{
printf("error\n");
return FALSE;
}
}
void GetNodeVal_and_Del(DuLinklist *L, int *dat, int num)
{
DuLinklist p;
int i;
p = (*L);
for(i=0; i<num; i++)
{
p = p->next;
dat[i] = p->data;
}
DeleteNumsNode(L, num);
for(i=0; i<num; i++)
printf("dat[%d]=%d ",i,dat[i]);
printf("\n");
}
void TraverseList(DuLinklist *L){
DuLinklist p;
p = (*L);
while(p->next != NULL){
printf("%d ", p->next->data);
p = p->next;
}
printf("\n\n");
}
void ReverTraverse(DuLinklist *L){
DuLinklist p;
p = GetLast(L);
while(p->prior != NULL){
printf("%d ", p->data);
p = p->prior;
}
printf("\n\n");
}
int main()
{
int n,i;
int num;
int buf[64];
DuLinklist La;
while(1)
{
printf("==========================================\r\n");
printf(" 双向链表操作\r\n");
printf("==========================================\r\n");
printf("1:创建双向链表\r\n");
printf("2:向头结点添加数据\r\n");
printf("3:向尾结点添加数据\r\n");
printf("4:向指定位置插入数据\r\n");
printf("5:获取指定位置数据\r\n");
printf("6:从头部获取几组数据并删除\r\n");
printf("7:删除指定位置的数据\r\n");
printf("8:删除连续几个节点\r\n");
printf("9:顺序遍历链表\r\n");
printf("10:逆序遍历链表\r\n");
printf("11:获取链表的长度\r\n");
printf("12:退出操作\r\n");
printf("------------------------------------------\r\n");
printf("请输入操作码:");
scanf("%d",&n);
switch(n)
{
case 1:
printf("创建链表长度:");
scanf("%d",&num);
CreateTailList(&La, num);
break;
case 2:
printf("从头结点添加节点数:");
scanf("%d",&num);
printf("输入数据:");
for(i=0; i<num; i++)
scanf("%d",&buf[i]);
InsertHeadNode(&La, buf, num);
break;
case 3:
printf("从尾结点添加节点数:");
scanf("%d",&num);
printf("输入数据:");
for(i=0; i<num; i++)
scanf("%d",&buf[i]);
InsertTailNode(&La, buf, num);
break;
case 4:
printf("输入插入位置:");
scanf("%d",&num);
printf("输入插入数据:");
scanf("%d",&buf[0]);
InsertElem(&La,num, buf[0]);
break;
case 5:
printf("输入获取位置:");
scanf("%d",&num);
GetElem(&La,num);
break;
case 6:
printf("输入获取数目:");
scanf("%d",&num);
DeleteNumsNode(&La,num);
break;
case 7:
printf("输入删除位置:");
scanf("%d",&num);
DeleteElem(&La,num);
break;
case 8:
printf("输入删除数目:");
scanf("%d",&num);
DeleteNumsNode(&La,num);
break;
case 9:
printf("顺序遍历链表:\n");
TraverseList(&La);
break;
case 10:
printf("逆序遍历链表:\n");
ReverTraverse(&La);
break;
case 11:
printf("当前链表长度:%d\n",GetLen(&La));
break;
case 12:
printf("退出操作!\n");
return 0;
default:
printf("输入出错,程序终止\n");
return 0;
}
}
return 0;
}