链表的C语言实现

#ifndef _CONST_H_
#define _CONST_H_

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

typedef enum
{
  False = 0,
  True,
}Bool;

typedef int ElemType;

#define QUEUE_MAX_SIZE 10

#define STACK_INIT_SIZE 10
#define STACK_INCREMENT_SIZE 2

#define Null ((void *)0)

typedef enum
{
  NORMAL = 0,
  ERROR,
  UNDERFLOW,
  OVERFLOW,
  STATUSCOUNT,
}Status;

#endif

#ifndef _LINKED_TABLE_H_
#define _LINKED_TABLE_H_

#include "Const.h"

typedef struct linkednode
{
  ElemType data;
  struct linkednode *pNext;
  struct linkednode *pPrev;
}LinkedNode, *pLinkedNode;

pLinkedNode InitLinkedTable();

Bool IsLinkedTableEmpty(pLinkedNode pLN);

Status AddNodeInHead(pLinkedNode pLN, ElemType elem);

Status AddNodeInTail(pLinkedNode pLN, ElemType elem);

void TraverseLinkedTable(pLinkedNode pLN);

void DestoryLinkedTable(pLinkedNode pLN);

int GetLinkedTableLength(pLinkedNode pLN);

void RemoveElemInLinkedTable(pLinkedNode pLN, ElemType elem);

#endif

#include "LinkedTable.h"

pLinkedNode InitLinkedTable()
{
  pLinkedNode pLN = (pLinkedNode)malloc(sizeof(LinkedNode));
  if (pLN == Null)
  {
    printf("No accessable free memeory.\n");
    return Null;
  }
  //data in head node is used to store linked table data count
  pLN->data = 0;
  pLN->pNext = Null;
  return pLN;
}

Bool IsLinkedTableEmpty(pLinkedNode pLN)
{
pLinkedNode pHead = pLN;
if (pHead->pNext == Null)
{
return True;
}
else
{
return False;
}
}

Status AddNodeInHead(pLinkedNode pLN, ElemType elem)
{
//printf("In Function AddNodeInHead.\n");
pLinkedNode pHead = pLN;
pLinkedNode pTempNode = (pLinkedNode)malloc(sizeof(LinkedNode));
if (pTempNode == Null)
{
printf("Can not add the node for no accessable free memory.\n");
return ERROR;
}
pTempNode->data = elem;
pTempNode->pNext = pHead->pNext;

pHead->data += 1;
pHead->pNext = pTempNode;

return NORMAL;
}

Status AddNodeInTail(pLinkedNode pLN, ElemType elem)
{
//printf("In Function AddNodeInTail.\n");
pLinkedNode pHead = pLN;
pLinkedNode pTempNode = (pLinkedNode)malloc(sizeof(LinkedNode));
if (pTempNode == Null)
{
printf("Can not add the node for no accessable free memory.\n");
return ERROR;
}
pHead->data += 1;
if (IsLinkedTableEmpty(pHead))
{
pHead->pNext = pTempNode;
}
else
{
pLinkedNode pTemp = pHead;
while (pTemp->pNext != Null)
{
pTemp = pTemp->pNext;
}
pTemp->pNext = pTempNode;
}
pTempNode->data = elem;
pTempNode->pNext = Null;

return NORMAL;
}

void TraverseLinkedTable(pLinkedNode pLN)
{
if(IsLinkedTableEmpty(pLN))
{
printf("The Linked Table Is Empty.\n");
}
else
{
pLinkedNode pHead = pLN;
pLinkedNode pTemp = pHead->pNext;
while(pTemp != Null)
{
printf("Value: %d\n", pTemp->data);
pTemp = pTemp->pNext;
}
}
}

void DestoryLinkedTable(pLinkedNode pLN)
{
if(IsLinkedTableEmpty(pLN))
{
printf("The Linked Table Is Empty.\n");
free(pLN);
}
else
{
pLinkedNode pHead = pLN;
pLinkedNode q;
pLinkedNode pTemp = pHead;
while(pTemp != Null)
{
q = pTemp->pNext;
free(pTemp);
pTemp = q;
}
}
}

int GetLinkedTableLength(pLinkedNode pLN)
{
return pLN->data;
}

void SortLinkedTable(pLinkedNode pLN)
{

}

void RemoveElemInLinkedTable(pLinkedNode pLN, ElemType elem)
{
if (IsLinkedTableEmpty(pLN))
{
printf("The Linked Table Is Empty.\n");
}
else
{
Bool hasTargetNode = False;
pLinkedNode pHead = pLN;
pLinkedNode pTemp = pHead;
while(pTemp->pNext != Null)
{
if (pTemp->pNext->data == elem)
{
hasTargetNode = True;
pTemp->pNext = pTemp->pNext->pNext;
}
else
{
pTemp = pTemp->pNext;
}
}
if (hasTargetNode == False)
{
printf("The data is not in current Linked Table.\n");
}
else
{
printf("Elem %d has been removed successfully.\n", elem);
}
}
}

上一篇:在IT界取得成功应该知道的10件事


下一篇:【linux】ubuntu中上下左右键变为^[[A^[[B^[[D^[[C问题处理