线性表实现(链表)

链表结构

typedef int ElemType;

typedef int Bool;
#define True 1
#define False 0

typedef enum{
  success = 0,
  fail,
  range_error,
  fatal
}Status;


typedef struct Node{
  ElemType data;
  struct Node * next;
}Node;

线性表操作

  1. 初始化 :

时间复杂度: O(1)

Status init(Node *linkedTable){
  Status s = success;
  Node *header = (Node *)malloc(sizeof(Node));
  if(header){
    header -> next = NULL;
    linkedTable = header;
  }else{
    s = fatal;
  }
  return s;
}
  1. 清空 :

时间复杂度: O(n)

void clear(Node *linkedTable){
  Node *deleteNode;
  while(linkedTable -> next) {
    deleteNode = linkedTable->next;
    linkedTable->next = deleteNode ->next;
    free(deleteNode);
  }
}
  1. 销毁 :

时间复杂度: O(1)

void destroy(Node *linkedTable){
  clear(linkedTable);
  free(linkedTable); // 释放头节点
  linkedTable = NULL;
}
  1. 查询(按位置) :

时间复杂度: O(n)

Status retrieveByPos(Node *linkedTable, int pos, ElemType *ele){
  Status s = range_error;
  Node *ptr = linkedTable -> next;  //ptr point to the first node
  int i = 1;

  while(ptr && i < pos) {
    i++;
    ptr = ptr ->next;
  }

  if(i == pos && ptr) {
    *ele = ptr->data;
  }
  return s;
}
  1. 查询(按值) :

时间复杂度: O(n)

Status locateByValue(Node *linkedTable, ElemType value ,int *pos){
  Status s = range_error;
  Node *ptr = linkedTable->next;
  int i = 1;
  while(ptr && ptr->data != value) {
    i++;
    ptr = ptr->next;
  }
  if(ptr){
    *pos = i;
    s = success;
  }
  return s;
}
  1. 插入 :
  • 时间复杂度: O(n).
Status insertByPos(Node *linkedTable, int pos, ElemType ele){
  Status s = range_error;
  Node *ptr = linkedTable; //这里不能指向第一元素,因为可能删除第一个元素
  int i = 0;
  while(i < pos -1 && ptr) {
    i++;
    ptr = ptr->next;
  }

  if (i == pos - 1 && ptr) {
    Node *newNode = (Node *) malloc(sizeof(Node));
    if(newNode) {
      newNode -> data = ele;
      newNode -> next = ptr -> next;
      ptr ->next = newNode;
      s = success;
    }else{
      s = fatal;
    }
  }

  return s;
}
  1. 删除 :
  • 时间复杂度: O(n)
Status deleteyPos(Node *linkedTable, int pos){
  Status s= range_error;
  Node *ptr = linkedTable; //这里不能指向第一元素,因为可能删除第一个元素
  int i = 0;
  while(i < pos -1 && ptr) {
    i++;
    ptr = ptr->next;
  }
  if(i == pos -1 && ptr) {    //判断存在第pos-1元素存在
    if(ptr -> next != NULL){  //第pos元素(要删除的元素)存在
      Node * deleteNode = ptr -> next;
      ptr -> next = ptr -> next -> next;
      free(deleteNode);
      deleteNode = NULL;
      s = success;
    }

  }

  return s;
}
  1. 判断是否为空 :

时间复杂度: O(1)

Bool isEmpty(Node *linkedTable){
  return (linkedTable->next == NULL)? True:False;
}
  1. 获取线性表大小 :

时间复杂度: O(n)

int getLength(Node *linkedTable){
  int i = 0;
  Node *ptr = linkedTable->next;
  while(ptr) {
    i++;
    ptr = ptr -> next;
  }
  return i;
}
  1. 尾部插入 :

时间复杂度: O(n)

Status appendList(PNode header,int data){
  Status s = success;
  Node *ptr = header;
  while(ptr && ptr->next != NULL){
    ptr = ptr ->next;
  }
  Node *tmp = (Node *) malloc(sizeof(Node));
  if(tmp){
    tmp -> data = data;
    tmp -> next = NULL;
    ptr -> next = tmp;
  } else{
    s = fatal;
  }
  return s;
}

猜你喜欢

转载自blog.csdn.net/kelly_fumiao/article/details/85227317
今日推荐