[数据结构与算法分析] 单链表基本操作的实现

  这几天还在啃Weiss书的Ch.3,随手把书上单链表基本操作的代码打了一遍,顺便补充了一点自己写的东西(一堆注释以及几个函数),经过测试应该是没问题。

  这次尝试用所谓的"Google Style"写代码,习惯了缩进4空格的Windows风格后再改到缩进2空格,真的是有些不习惯。本来Google Style中,变量应该都是小写字母,但我实在不喜欢小写的L和P,变量命名仍就坚持自己的习惯——单字母变量大写,多字母小写。

  需要注意的一些地方:

  1,在几个函数中出现了类似while (P != NULL && P->Element != X) 这样子的代码,根据逻辑运算符的短路规则,如果P != NULL(链表末尾结点),就不会再检查后面的布尔表达式。

  2,原书使用  Position tmp = malloc(sizeof(struct Node)) 这样的代码,但根据Google Style 建议,改为" malloc(sizeof(tmp))"有利于保持同步(鉴于tmp的数据类型有可能改变)。



  Code is here~

  .h头文件:

#ifndef LINKLIST_H_INCLUDED
#define LINKLIST_H_INCLUDED

struct Node;
typedef struct Node *PtrToNode;
//  List and Position are pointers
typedef PtrToNode List;
typedef PtrToNode Position;

#define ElementType int//set the type of element as Integer

//  Functions:
List MakeEmpty(List L);
List InitialList();
int IsEmpty(List L);
int IsLast(Position P, List L);
Position Find(ElementType X, List L);
void Delete(ElementType X, List L);
Position FindPrevious(ElementType X, List L);
void Insert(ElementType X, List L, Position P);
void DeleteList(List L);
Position Header(List L);
Position First(List L);
Position Last(List L);
Position Advance(Position P);//not implemented yet
ElementType Retrive(Position P);//not implemented yet
void PrintElement(Position P);
void PrintList(List L);
int SizeOfList(List L);
int FindIndex(ElementType X, List L);
int InsertAsTail(ElementType X, List L);
int InsertAsHead(ElementType X, List L);

#endif // LINKLIST_H_INCLUDED


  

  .c文件:

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

struct Node {
  ElementType Element;
  Position Next;
};

List MakeEmpty(List L) {
  //used for initialization
  Position P = Header(L);
  P->Next = NULL;
  P->Element = 0;
}
List InitialList() {//return a new List (header)

  List tmp;
  tmp = malloc(sizeof(tmp));//standard malloc way for "tmp"
  tmp->Next = NULL;
  //  tmp = MakeEmpty(tmp);
  return tmp;
}
int IsEmpty(List L) {//Return true if L is empty
  return L->Next == NULL;
}
int IsLast(Position P, List L) {//Return true if P is the last position of the list L
  return P->Next == NULL;
}
Position Header(List L) {
  Position P = L;
}
Position First(List L) {
  Position P = L->Next;
}
Position Last(List L) {
  Position P = Header(L);
  while (!IsLast(P,L)) {
    P = P->Next;
  }
  return P;
}
Position Find(ElementType X, List L) {
//  Return Position of X in L;NULL if not found;
  Position P = L->Next;
  while (P != NULL && P->Element != X)
    //  You should first know if P is NULL, here we use shortcut "&&"
    P = P->Next;
  return P;
}
int FindIndex(ElementType X, List L) {
  //Return Index of X in L;-1 if not found;Index starts from 1(not 0).
  int index = 1;
  Position P = L->Next;
  while (P != NULL && P->Element != X) {
    //  You should first know if P is NULL, here we use shortcut "&&"
    P = P->Next;
    index++;
  }
  return index; //header node is not counted in index
}
void Delete(ElementType X, List L) {//Delete the first occurrence of X in List L
  Position P,tmp;
  P = FindPrevious(X,L);
  if (!IsLast(P,L)) {
    tmp = P->Next;  //Now "tmp" is just which node you want to "delete" here
    P->Next = tmp->Next;
    free(tmp);  //the data at address tmp is meaningless after free it
  }
}
Position FindPrevious(ElementType X, List L) {
  //If X is not found, then Next Field of Returned Position is NULL
  Position P = L;
  while(P->Next != NULL && P->Next->Element != X)
    P = P->Next;
  return P; //If X not found in this List, P is the last node, P->Next is NULL
}
void Insert(ElementType X, List L, Position P) {//Insert After Position P
  Position tmp = malloc(sizeof(tmp));
  if (tmp == NULL) {
    printf("Out of space!");  //  TODO(AllZY): Use a variable to indicate the state.
  } else {
    tmp->Element = X;
    tmp->Next = P->Next;
    P->Next = tmp;  //tmp should never be "NULL" when this line executed
  }
}
int InsertAsTail(ElementType X, List L) {
//  return index if inserted successfully, otherwise return -1
//  Insert it after the last ele.
  Position Tail = Last(L);
  Position tmp = malloc(sizeof(tmp));
  if (tmp == NULL) {
    return -1;
  } else {
    tmp->Element = X;
    tmp->Next = Tail->Next;
    Tail->Next = tmp;
  } //Is this what they called "google style"? I think it's a bit odd...
}
int InsertAsHead(ElementType X, List L) {
//  return index if inserted successfully, otherwise return -1
//  Insert it before the first ele.
  Position Head = Header(L);
  Position tmp = malloc(sizeof(tmp));
  if (tmp == NULL) {
    return -1;
  } else {
    tmp->Element = X;
    tmp->Next = Head->Next;
    Head->Next = tmp;
  }
}
void DeleteList(List L) {
  Position P,tmp;
  P = L->Next;
  L->Next = NULL;
  while (P != NULL) {
    tmp = P->Next;
    free(P);
    P = tmp;
  }
  /*
  //an unreliable version, not recommended
  while(P!=NULL)
  {
    free(P);
    P = P->Next;
  }
  */
}
void PrintElement(Position P) {//Print one Element
  ElementType e = P->Element;
  printf("%d ",e);
}
void PrintList(List L) {//Print all elements in the List
  Position P = First(L);
  while (P != NULL) {
    PrintElement(P);
    P = P->Next;
  }
}
int SizeOfList(List L) {
  int count = 0;
  Position P = First(L);
  while (P != NULL) {
    count++;
    P = P->Next;
  }
  return count;
}

int main()
{
//  List L = malloc(sizeof (List));
//  MakeEmpty(L);
  List L = InitialList();
  Position P = Header(L);
  int i;
  for (i = 1; i <= 10; i++) {
    InsertAsTail(i,L);
  }
  for ( ; i <= 20; i++) {
    InsertAsHead(i,L);
  }

  PrintList(L);
  printf("\nSize of list is %d",SizeOfList(L));
  Position p10 = Find(10,L);
  printf("\nThe element in the Position of value \"10\" is %d\n",p10->Element);
  printf("The index of Element 11 is %d\n",FindIndex(11,L));

  DeleteList(L);
  return 0;
}

  测试运行结果:

  

 

参考资料:

《数据结构与算法分析》

http://google.github.io/styleguide/cppguide.html

http://zh-google-styleguide.readthedocs.org/en/latest/google-cpp-styleguide/

———————————————————————————————————————————

原创文章,转载请注明出处: http://www.cnblogs.com/allzy/

你可能感兴趣的:(数据结构)