wwtsanmao 发表于 2009-12-4 13:28:09

list.h1

#include <string.h>
#include <stdio.h>
#include <iostream.h>
#include "base.h"
//整型链表类
#ifndef LIST_H
#define LIST_H
class list
{
protected:
list *head;
list *tail;
list *next;
list *freepoint; //游标指针
int m_num; //成员的值
int m_count;//成员数量
public:
list ()
{
head = tail = next = freepoint = NULL;
m_count = 0;
}
list (int value)
{
head = tail = next = freepoint = NULL;
m_count = 0;
Append (value);
}
list (list& _list)
{
head = tail = next = freepoint = NULL;
m_count = 0;
for (int i = 1;i <= _list.Count();i++)
{
   Append(_list.Get(i));
}
}

/*########################*/
//void list::Insert (int value,int pos)
//插入链表中的指定位置
/*########################*/
void list::Insert (int value,int pos)
{
//插入的位置大于现有成员数量
if (pos >= Count())
{
   Append(value);
   return;
}
//插入到第1个位置
else if (pos == 1)
{
   list *item;
   item = new (list);
   if (! item)
   {
    PRINT("Allocation error!");
    return;
   }
   item->m_num = value;
   item->next = head;
   head = item;
   m_count++;//成员数加1
   return;
}
//插入到中间位置
else
{
   list *tmp;
   tmp = head;
   int t_pos = 1;
   while (tmp)
   {
    if ((t_pos + 1) == pos)
    {
   list *item;
   item = new (list);
   if (! item)
   {
      PRINT("Allocation error!");
      return;
   }
   item->m_num = value;
   
   list&front = *tmp;//指向要插入位置的前一个成员   
   tmp = tmp->next;//指向要插入的位置;
   front.next = item;
   item->next = tmp;
   m_count++;//成员数加1
   return;
    }
    tmp=tmp->next;
    t_pos++;
   }
}   
}
/*########################*/
//void Close ()
//修改成员的值
/*########################*/
void Close ()
{
list* tmp;
tmp = head;
while (head)
{
   tmp = head;
   head = head->next;
   delete tmp;
}
head = tail = next = freepoint = NULL;
m_count = 0;
m_num = NULL;
return;
}
/*########################*/
//void Set (int value,int pos)
//修改成员的值
/*########################*/
void Set (int value,int pos)
{
list* tmp;
tmp = head;
int i = 1;
while (tmp)
{
   if(pos == i)
   {
    tmp->m_num = value;
    return;
   }
   tmp = tmp->next;
   i++;
}
PRINT("member is not exist!");
return;
}
/*########################*/
//void Append (int value)
//按顺序加入链表
/*########################*/
void Append (int value)
{
list *item;
item = new list;
if (! item)
{
   PRINT("Allocation error!");
   return;
}
item->m_num = value;
if (tail)
{
   tail->next = item;
}
item->head = tail;
tail = item;
item->next = NULL;
if (! head)
{
   head = tail;
}
m_count++;//成员数加1
}
/*########################*/
//int Find (int value)
//在链子表中查找,找到返回下标,否则返回-1
/*########################*/
int Find (int value)
{
list* tmp;
tmp = head;
int pos = 1;
while (tmp)
{
   if(value == tmp->m_num)
   {
    return pos;
   }
   tmp = tmp->next;
   pos++;
}
return -1;
}
/*########################*/
//int Retrieve ()
//从前往后出链表
/*########################*/
int Retrieve ()
{
if (head)
{
   int tmp;
   list* p;
   p = head;
   tmp = head->m_num;
   head = head->next;
   delete p;
   PRINT(tmp)
   return tmp;
}
else
{
   PRINT("list is empty!");
   return NULL;
}
}
/*########################*/
//int Get (int pos)
//取得链表中指定成员,下标从1开始
/*########################*/
int Get (int pos)
{
list* tmp;
tmp = head;
int t_pos = 1;
while (tmp)
{
   if (pos == t_pos)
   {
    //PRINT(tmp->m_num);
    return tmp->m_num;
   }
   t_pos++;
   tmp = tmp->next;
}
PRINT("member is not exist!");
return NULL;
}
/*########################*/
//int First ()
//取得链表第一个成员
/*########################*/
int First ()
{
if (head)
{
   freepoint = head->next;
   return head->m_num;
}
PRINT("member is not exist!");
return NULL;
}
页: [1]
查看完整版本: list.h1