单链表的实现2

list.h头文件

typedef int elementType;

#ifndef LIST_H
#define 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);
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;
};


list.c文件

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

// #define true 1
// #define false 0

// 这是要干啥呀?
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;
}

// 在链表的位置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;
    tempPosition->next = aNode;
  }
  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;
    free(P);
  }
  else
  {
    fprintf(stderr, "链表 L 中没有带有数据 X 的节点 \n");
  }

}


position findPrevious(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)
  {
    return prevP;
  }
  else
  {
    fprintf(stderr, "链表 L 中没有带有数据 X 的节点 \n");
    return NULL;
  }

}


void deleteList(list L)
{
  position P;

  while (L->next != NULL)
  {
    P = L->next;
    L->next = L->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;
  printf("<%d> \t--> %d (header) \n", i, L->element);
  while(P != NULL)
  {
    i++;
    printf("<%d> \t--> %d  \n", i, P->element);
    P = P->next;
  }
  printf("\n");
}

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

测试文件

#include"list.c"
int main()
{
  list L = generateList(30000);
  displayList(L);
  printf("find 88 is %d \n\n ", find(88, L)->element);
  deleteList(L);
  displayList(L);
  return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值