⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 alist.cpp

📁 经典c++程序的实现
💻 CPP
字号:

// alist.cpp

#include <stdio.h>
#include <iostream.h>
#include <assert.h>

#include "..\include\book.h"

typedef int ELEM;

#include "..\include\alist.h" // Load the member functions for list

list::list(const int sz)           // Constructor: initialize
  { msize = sz;  numinlist = curr = 0; listarray = new ELEM[sz]; }

list::~list()                      // Destructor: return array space
  { delete [] listarray; }

void list::clear()                 // Remove all ELEMs from list
  { numinlist = curr = 0; }        // Simply reinitialize values


// Insert ELEM at current position
void list::insert(const ELEM& item) {
  // Array must not be full and curr must be a legal position
  assert((numinlist < msize) && (curr >=0) && (curr <= numinlist));
  for(int i=numinlist; i>curr; i--) // Shift ELEMs up to make room
    listarray[i] = listarray[i-1];
  listarray[curr] = item;
  numinlist++;                     // Increment current list size
}

void list::append(const ELEM& item) { // Insert ELEM at tail of list
  assert(numinlist < msize);          // List must not be full
  listarray[numinlist++] = item;      // Increment list size
}

ELEM list::remove() {              // Remove and return current ELEM
  assert(!isEmpty() && isInList());   // Must be an ELEM to remove
  ELEM temp = listarray[curr];        // Store removed ELEM
  for(int i=curr; i<numinlist-1; i++) // Shift elements down
    listarray[i] = listarray[i+1];
  numinlist--;                        // Decrement current list size
  return temp;
}

void list::setFirst()              // Set curr to first position
  { curr = 0; }

void list::prev()                  // Move curr to previous position
  { curr--; }

void list::next()                  // Move curr to next position
  { curr++; }

int list::length() const           // Return current length of list
  { return numinlist; }

void list::setPos(const int pos)   // Set curr to specified position
  {curr = pos; }

void list::setValue(const ELEM& val) { // Set current ELEM's value
  assert(isInList());              // Curr must be at valid position
  listarray[curr] = val;
}

ELEM list::currValue() const {     // Return current ELEM's value
  assert(isInList());              // Must be at a valid position
  return listarray[curr];
}

bool list::isEmpty() const         // Return TRUE if list is empty
  { return numinlist == 0; }

bool list::isInList() const        // TRUE if curr is within list
  { return (curr >= 0) && (curr < numinlist); }

bool list::find(const ELEM& val) { // Find value (starting at curr)
  while (isInList())               // Stop if reach end
    if (currValue() == val) return TRUE;  // Found it
    else next();
  return FALSE;                    // Not found
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -