双向链表的实现

下面是双向链表的实现,《数据结构与算法分析C语言描述》中的
doubleList.h

typedef int elementType;

#ifndef DOUBLE_LIST_H
#define DOUBLE_LIST_H

struct node;
typedef struct node * ptrToNode;
typedef ptrToNode list;
typedef ptrToNode position;

list makeEmpty(list L); 
int isEmpty(list L);
int isLast(list L, position P);
position find(elementType X, list L);
position findLast(list L);
void insert(list L, elementType X, position P);
void deleteElement(elementType X, list L);
position findPrevious(elementType X, list L);
void deleteList(list L);
position header(list L);
position first(list L);
position advance(position P);
void displayList(list L);
list generateList(int length);

#endif

struct node
{
  elementType element;
  position next;
  position prev;
};

doubleList.c

#include"doubleList.h"
#include<stdlib.h>
#include<stdio.h>


// 这是要干啥呀?
list makeEmpty(list L)
{
 return L;
}

int isEmpty(list L)
{
  return L->next == NULL;  
}

// 这里 L 没有什么作用
int isLast(list L, position P)
{
  return P->next == NULL;
}

// 返回节点X的地址,如果没有找到X,那么返回NULL
position find(elementType X, list L)
{
  position P;
  P = L->next;
  
  while (!isLast(L, P))
  {
    if(P->element == X)
      return P;
    else
      P = P->next;
  }

  return NULL;
}


position findLast(list L)
{
  position P;
  P = L->next;

  while (P->next != NULL)
  { 
    P = P->next;
  }
  return P;  
}


// 在链表的位置P之后插入一个新的节点,
void insert(list L, elementType X, position P)
{
  position tempPosition;
  tempPosition = L;

  //确认链表L中是否存在节点 P
  while(tempPosition != NULL && tempPosition != P)
  {
    tempPosition = tempPosition->next;
  }

  if(tempPosition != NULL)
  {
    // 在内存中申请一个节点
    position aNode = (position)malloc(sizeof(struct node));
    aNode->element = X;
    aNode->next = tempPosition->next;
    if(tempPosition->next != NULL)
      tempPosition->next->prev = aNode;
    tempPosition->next = aNode;
    aNode->prev = tempPosition;
    // if (aNode->next == NULL)
    //   printf("inserted node is %d ,it prev is %d, it next is not exit \n",aNode->element, aNode->prev->element);
    // else
    //   printf("inserted node is %d ,it prev is %d, it next is %d \n",aNode->element, aNode->prev->element, aNode->next->element);

  }
  else
  {
    fprintf(stderr, "链表 L 中没有节点 P \n");
  }
}


void deleteElement(elementType X, list L)
{
  position P, prevP;
  P = L->next;
  prevP = L;

  while (P != NULL && P->element != X)
  {
    prevP = P;
    P = P->next;
  }
  
  if(P != NULL)
  {
    prevP->next = P->next;
    P->next->prev =prevP; 
    free(P);
  }
  else
  {
    fprintf(stderr, "链表 L 中没有带有数据 X 的节点 \n");
  }

}


position findPrevious(elementType X, list L)
{
  position P;
  P = L->next;

  while (P != NULL && P->element != X)
  {
    P = P->next;
  }

  if(P != NULL)
  {
    return P->prev;
  }
  else
  {
    fprintf(stderr, "链表 L 中没有带有数据 X 的节点 \n");
    return NULL;
  }

}


void deleteList(list L)
{
  list tempList = L;
  position P;

  while (tempList->next != NULL)
  {
    P = tempList->next;
    tempList->next = tempList->next->next;
    free(P);
  }
}


position header(list L)
{
  return L;
}


position first(list L)
{
  return L->next;
}


position advance(position P)
{
  return P;
}


void displayList(list L)
{
  position P;
  P = L->next;

  int i = 0;
  if(L->next == NULL)
    printf("<%d> \t--> %d (header), next is not exit, prev is not exit \n", i, L->element);
  else
    printf("<%d> \t--> %d (header), next is %d, prev is not exit \n", i, L->element, L->next->element);

  while(P != NULL)
  {
    i++;
    if (P->next != NULL)
      printf("<%d> \t--> %d, next is %d, prev is %d \n", i, P->element, P->next->element, P->prev->element);
    else
      printf("<%d> \t--> %d, next is not exit, prev is %d \n", i, P->element, P->prev->element);      
    P = P->next;
  }
  printf("\n");
}


// 生成长度为length的单链表
list generateList(int length)
{
  list L = (position)malloc(sizeof(struct node));
  L->next = NULL;
  L->element = 0;
  L->prev = NULL;
  
  int i = 0;
  for(i = 0; i < length; i++)
  {
    insert(L, random() % 100,L);
  }
  printf("\n");
  return L;
}

doubleListTest.c

#include"doubleList.c"
#include<stdio.h>
#define N 10

int main()
{
  list L = generateList(N);
  displayList(L);

  position lastNode = findLast(L);
  printf("last node is %d \n", lastNode->element);
  position tempNode = lastNode;

  int i = N;
  while(tempNode != L)
  {
    printf("<%d> \t is %d \n", i, tempNode->element);
    i--;
    tempNode = tempNode->prev;
  }
  printf("<%d> \t(header) is %d \n \n", i, tempNode->element);

  printf("find prev of 15 is %d \n", findPrevious(15, L)->element);
  deleteElement(15, L);
  
  // deleteList(L);
  
  displayList(L);
  return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值