循环链表实现

本文详细介绍了使用C语言实现的循环链表,包括数据结构定义、关键函数如创建、插入、删除及遍历操作,适合学习者理解链表基础和实践编程。

下面是《数据结构与算法C语言实现》的循环链表实现

cycleList.h

typedef int elementType;

#ifndef CYCLE_LIST_H
#define CYCLE_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;
};

cycleList.c

#include"cycleList.h"
#include<stdlib.h>
#include<stdio.h>
#include<assert.h>


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

position find(elementType X, list L)
{
  if(isEmpty(L))
  {
    return NULL;
  }
  else
  {
    position P = L->next;

    while (P->element != X)
    {
      P = P->next;
      if (P == L->next)
        return NULL;
    }
    return P;
  }

}

// 在位置P之后插入节点 X
void insert(list L, elementType X, position P)
{
  if (isEmpty(L))
  {
    assert(L == P);
    position aNode = (position)malloc(sizeof(struct node));
    L->next = aNode;
    L->prev = NULL;
    aNode->prev = aNode;
    aNode->next = aNode;
    aNode->element = X;
  }
  else
  {
    position tmpPosition = L->next;

    while (tmpPosition != P)
    {
      tmpPosition = tmpPosition->next;

      if (tmpPosition == L->next)
      {
        printf("error,,在链表中没有位置P\n");
        getchar();
        break;
      }
    }

    // 此时,在链表中找到了位置 P,在 P 的后面插入aNode
    position aNode = (position)malloc(sizeof(struct node));
    aNode->next = tmpPosition->next;
    aNode->prev = tmpPosition;
    tmpPosition->next->prev = aNode;
    tmpPosition->next = aNode;
    aNode->element = X;
  }
}

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

    if(P->next == P && P->element == X)
    {
      L->next = NULL;
      printf("deleteElement: delete final %d, it addr is %x, L is %x \n", X, P, L);
      free(P);
    }
    else
    {
      while (P->element != X)
      {
        P = P->next;
        if(L->next == P)
          return;
      }

      // 这时,找到了节点 X ,地址为 P ,并删除节点
      if(L->next == P)
      {
        L->next = P->next;
        printf("deleteElement: delete %d, it addr is %x, L is %x \n", X, P, L);
        (P->prev)->next = P->next;
        (P->next)->prev = P->prev;
        // P = P->next;
        // P = P->prev;
      }
      else
      {
        printf("deleteElement: delete %d, it addr is %x, L is %x \n", X, P, L);
        (P->prev)->next = P->next;
        (P->next)->prev = P->prev;
        // P = P->next;
        // P = P->prev;
      }

    }
  }
}

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

    while (P->element != X)
    {
      P = P->next;
      if(P == L->next)
      {
        printf("链表 L 中没有节点 X \n");
        return NULL;
      }
    }

    return P->prev;
  }
  else
  {
    return NULL;
  }
}

void deleteList(list L)
{
  if(!isEmpty(L))
  {
    position P;
    P = L->next;

    while (P->next != P)
    {
      // printf("deleteList: delete addr is %x, L's addr is %x\n", P, L);
      fflush(stdout);

      deleteElement(P->element, L);
      P = P->next;
    }
    deleteElement(P->element, L);
  }
}

void displayList(list L)
{
  printf("header is (%d), it addr is %x, it next is %x \n", L->element, L, L->next);
  if(!isEmpty(L))
  {
    position P;
    P = L->next;

    int i = 0;
    do
    {
      i++;
      printf("<%d> \t is (%d) \t %x, prev is %x, next is %x\n", i, P->element, P, P->prev, P->next);
      P = P->next;
    }while(P != L->next);
  }
}



list generateList(int length)
{
  if (length >= 1)
  { 
    list L = (position)malloc(sizeof(struct node));
    L->prev = NULL;
    L->next = NULL;
    
    int X = random() % 100;
    insert(L, X, L);
    // printf(" %d <->", X);

    for (int i = 0; i < length -1; i++)
    {
      X = random() % 100;
      // printf(" %d <->", X);
      insert(L, X, L->next);
    }
    printf("\n");
    return L;
  }
  else
  {
    printf("干嘛呢? length <= 0 \n");
    return NULL;
  }
  
}

cycleListTest.c

#include"cycleList.c"

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

  deleteList(L);
  displayList(L);
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值