单向循环链表C/C++版

最近看算法与数据结构,看到了单向循环链表,因由双向循环链表,操作更加方便,但是作为学习,还是应该知道掌握,理解其操作原理和简单实现算法。但是在网络上发现这方面的完整且正确的操作很少,故把自己写的一个简单的单向循环链表贴出来,以供大家学习交流之用,希望对初学者有一定的帮助。

该链表程序简单介绍:实现了链表节点插入、删除,已经清空整个链表,打印链表,和统计节点数的操作。

源程序如下:

/*************************************************************************
* Copyright(c)2009-2010 Company Name
* All rights reserved

* 文件名称:RouterList.cpp
* 简要描述:该程序实现了单向循环链表的初始化、插入、删除、打印等基本操作
*     此程序仅用于学习交流之用,有问题可E-MAIL:[email protected]

* 当前版本:2.0
* 作者/修改者:王发修(Acekiller)
* 完成日期:2009.8.29
* 修订说明:编写、调试环境Visual studio 2005
*
* 取代版本:1.0
* 修改人:
* 完成日期:
* 修订说明:
*************************************************************************/
#include
#include
#include

using namespace std;

#ifdef _MSC_VER
#pragma pack(push, 4)
#endif

typedef struct _ListNode{
 unsigned int value;
 struct _ListNode *next;
}ListNode, *pListNode;

#ifdef _MSC_VER
#pragma pack(pop, 4)
#endif

pListNode InitialList(void);
void InsertNewNode(pListNode h, unsigned int data);
void DeleteNode(pListNode h, unsigned int data);
unsigned int CountNode(pListNode h);
void ShowList(pListNode h);
void ClearList(pListNode h);

int main(int argc, char **argv)
{
 pListNode head = NULL;
 head = InitialList();
//  head = (pListNode)malloc(sizeof(ListNode));
//  if (head == NULL)
//  {
//   cout<<"Out of memory!"<//   exit(1);
//  }//end of if
//  head->next = head;

 for (int i = 1; i<= 10; i++)
 {
  cout<  InsertNewNode(head, i);
 }
 cout< unsigned int nodeNumber = 0;
 nodeNumber = CountNode(head);
 cout<<"The node number is :"< ShowList(head);
  DeleteNode(head, 5);
  ShowList(head);
 ClearList(head);
 ShowList(head);
 system("pause");
 return 0;
}

/************************************************************************/
/* Initialize the list                                                  */
/************************************************************************/
pListNode InitialList(void)
{
 pListNode pNext = NULL;
 pNext = (pListNode)malloc(sizeof(ListNode));

 if (pNext == NULL)
 {
  cout<<"Out of memory!"<  exit(-1);
 }
 pNext->next = pNext; //The list's tail point to the list's head,become a circle.
 pNext->value = 0;
 return pNext;
}

/************************************************************************/
/* Insert a new member to the list.          */
/************************************************************************/
void InsertNewNode(pListNode h, unsigned int data)
{
 pListNode pNext = NULL;
 pNext = (pListNode)malloc(sizeof(ListNode));
 if (pNext == NULL)
 {
  cout<<"Out of memory!"<  exit(-1);
 }//end of if

 pNext->value = data;
 pNext->next = h->next;
 h->next = pNext;
 
 return;
}

/************************************************************************/
/* Delete a member from the list                                        */
/************************************************************************/
void DeleteNode(pListNode h, unsigned int data)
{
 pListNode pNext = NULL;
 pListNode pFont = NULL;

 pNext = h->next;
 pFont = h;
 if (pNext == h)
 {
  cout<<"The list is empty!"<  return;
 }//end of if

 while (data != pNext->value && pNext != h)
 {
  pFont = pNext;
  pNext = pNext->next;
 }//end of while

 if (pNext != h)
 {
  pFont->next = pNext->next;
  free(pNext);
  return;
 }//end of if

 cout<<"Haven't find the node!"< return;
}

/************************************************************************/
/* Count the list's member                                              */
/************************************************************************/
unsigned int CountNode(pListNode h)
{
 pListNode pNext = NULL;
 unsigned int number = 0;
 pNext = h->next;
 if (pNext == h)
 {
  cout<<"The list is empty!"<  return number;
 }//end of if

 while (pNext != h)
 {
  number++;
  pNext = pNext->next;
 }//end of while

 return number;
}

/************************************************************************/
/* Show the list member             */
/************************************************************************/
void ShowList(pListNode h)
{
 pListNode pNext = NULL;

 pNext = h->next;
 if (pNext == h)
 {
  cout<<"The list is empty!"<  return;
 }//end of if

 cout<<"List is: ";
 while (pNext != h)
 {
  cout<value<<" ";
  pNext = pNext->next;
 }//end of while

 cout< return;
}

/************************************************************************/
/* Clear the list                                                       */
/************************************************************************/
void ClearList(pListNode h)
{
 pListNode pNext = NULL;
 pListNode pFont = NULL;
 pNext = h->next;
 pFont = h;

 if (pNext == h)
 {
  cout<<"The list is empty!"<  return;
 }//end of if
 
 while (pNext != h)
 {
  pFont->next = pNext->next;
  free(pNext);
  pNext = pFont->next;
 }//end of while

 h->next = h;
 return;
}

你可能感兴趣的:(list,null,数据结构,算法,insert,delete)