src.all
来自「经典c++程序的实现」· ALL 代码 · 共 874 行 · 第 1/2 页
ALL
874 行
return temp; // Return value removed
}
void list::setFirst() // Set curr to first position
{ curr = head; }
void list::next() // Move curr to next position
{ if (curr != NULL) curr = curr->next; }
void list::prev() { // Move curr to previous position
link* temp = head;
if ((curr == NULL) || (curr == head)) // No previous ELEM
{ curr = NULL; return; } // so just return
while ((temp!=NULL) && (temp->next!=curr)) temp=temp->next;
curr = temp;
}
int list::length() const { // Return current length of list
int cnt = 0;
for (link* temp = head->next; temp != NULL; temp = temp->next)
cnt++; // Count the number of ELEMs
return cnt;
}
void list::setPos(const int pos) { // Set curr to specified position
curr = head;
for(int i=0; (curr!=NULL) && (i<pos); i++)
curr = curr->next;
}
void list::setValue(const ELEM& val) // Set current ELEM's value
{ assert(isInList()); curr->next->element = val; }
ELEM list::currValue() const // Return value of current ELEM
{ assert(isInList()); return curr->next->element; }
bool list::isEmpty() const // Return TRUE if list is empty
{ return head->next == NULL; }
bool list::isInList() const // TRUE if curr is within list
{ return (curr != NULL) && (curr->next != NULL); }
bool list::find(const ELEM& val) { // Find value (starting at curr)
while (isInList())
if (curr->next->element == val) return TRUE;
else curr = curr->next;
return FALSE; // Not found
}
// asmain.cpp
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\astack.h"
main()
{
Stack L1;
Stack L2(15);
L2.push(10);
L2.push(20);
L2.push(15);
while(!L2.isEmpty()) {
L1.push(L2.pop());
cout << L1.topValue()<< " ";
}
cout << "\n";
cout << "New top: " << L1.topValue() << "\n";
L1.clear();
cout << "That is all.\n";
return(0);
}
// lsmain.cpp
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\linkf.h"
// This creates space for the freelist variable
link* link::freelist = NULL;
void* link::operator new(size_t) { // Overload new operator
if (freelist == NULL) return ::new link; // Create new space
link* temp = freelist; // Otherwise, get from freelist
freelist = freelist->next;
return temp; // Return the link node
}
void link::operator delete(void* ptr) { // Overload delete operator
((link*)ptr)->next = freelist; // Put on freelist
freelist = (link*)ptr;
}
#include "..\include\lstack.h"
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
typedef int ELEM;
main()
{
Stack L1;
Stack L2(15);
L2.push(10);
L2.push(20);
L2.push(15);
while(!L2.isEmpty()) {
L1.push(L2.pop());
cout << L1.topValue()<< " ";
}
cout << "\n";
cout << "New top: " << L1.topValue() << "\n";
L1.clear();
cout << "That is all.\n";
return(0);
}
// aqmain.cpp
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
#include <string.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\aqueue.h"
main()
{
Queue L1;
Queue L2(15);
L2.enqueue(10);
L2.enqueue(20);
L2.enqueue(15);
while(!L2.isEmpty()) {
cout << L2.firstValue() << " ";
L1.enqueue(L2.dequeue());
}
cout << "\n";
cout << "New top: " << L1.firstValue() << "\n";
L1.clear();
return(0);
}
// lqmain.cpp
#include <stdio.h>
#include <assert.h>
#include <iostream.h>
#include <string.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\linkf.h"
// This creates space for the freelist variable
link* link::freelist = NULL;
void* link::operator new(size_t) { // Overload new operator
if (freelist == NULL) return ::new link; // Create new space
link* temp = freelist; // Otherwise, get from freelist
freelist = freelist->next;
return temp; // Return the link node
}
void link::operator delete(void* ptr) { // Overload delete operator
((link*)ptr)->next = freelist; // Put on freelist
freelist = (link*)ptr;
}
#include "..\include\lqueue.h"
main()
{
Queue L1;
Queue L2(15);
L2.enqueue(10);
L2.enqueue(20);
L2.enqueue(15);
while(!L2.isEmpty()) {
cout << L2.firstValue() << " ";
L1.enqueue(L2.dequeue());
}
cout << "\n";
cout << "New front: " << L1.firstValue() << "\n";
L1.clear();
return(0);
}
// dllist.cpp
#include <stdio.h>
#include <assert.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\dlinkf.h"
// This creates space for the freelist variable
link* link::freelist = NULL;
/*
link::link() {
freelist = NULL; }
*/
void* link::operator new(size_t) { // Overload new operator
if (freelist == NULL) return ::new link; // Create new space
link* temp = freelist; // Otherwise, get from freelist
freelist = freelist->next;
return temp; // Return the link node
}
void link::operator delete(void* ptr) { // Overload delete operator
((link*)ptr)->next = freelist; // Put on freelist
freelist = (link*)ptr;
}
#include "..\include\llist.h"
// Definitions for list class -- doubly-linked list implementation.
list::list(const int sz) // Constructor -- ignore sz
{ tail = head = curr = new link; }
list::~list() { // Destructor
while(head != NULL) { // Return link nodes to free store
curr = head;
head = head->next;
delete curr;
}
}
void list::clear() { // Remove all ELEMs from list
while (head->next != NULL) { // Return link nodes to free store
curr = head->next;
head->next = curr->next;
delete curr;
}
curr = tail = head;
}
// Insert ELEM at current position
void list::insert(const ELEM& item) {
assert(curr != NULL);
curr->next = new link(item, curr->next, curr);
if (curr->next->next != NULL)
curr->next->next->prev = curr->next;
if (tail == curr) tail = curr->next;
}
void list::append(const ELEM& item) { // Append ELEM at tail of list
tail->next = new link(item, NULL, tail);
tail = tail->next;
}
ELEM list::remove() { // Remove ELEM at current position
assert(isInList()); // Must be valid position in list
ELEM temp = curr->next->element;
link* ltemp = curr->next;
if (ltemp->next != NULL) ltemp->next->prev = curr;
else tail = curr; // Removed tail ELEM - change tail
curr->next = ltemp->next;
delete ltemp;
return temp;
}
void list::prev() // Move curr to previous position
{ if (curr != NULL) curr = curr->prev; }
void list::setFirst() // Set curr to first position
{ curr = head; }
void list::next() // Move curr to next position
{ if (curr != NULL) curr = curr->next; }
int list::length() const { // Return current length of list
int cnt = 0;
for (link* temp = head->next; temp != NULL; temp = temp->next)
cnt++; // Count the number of elements
return cnt;
}
void list::setPos(const int pos) { // Set curr to specified position
curr = head;
for(int i=0; (curr!=NULL) && (i<pos); i++)
curr = curr->next;
}
void list::setValue(const ELEM& val) // Set current ELEM's value
{ assert(isInList()); curr->next->element = val; }
ELEM list::currValue() const // Return value of current ELEM
{ assert(isInList()); return curr->next->element; }
bool list::isEmpty() const // Return TRUE if list is empty
{ return head->next == NULL; }
bool list::isInList() const // TRUE if curr is within list
{ return (curr != NULL) && (curr->next != NULL); }
bool list::find(const ELEM& val) { // Find value (from current position)
while (isInList())
if (currValue() == val) return TRUE;
else next();
return FALSE; // Not found
}
// dlmain.cpp
#include <iostream.h>
#include "..\include\book.h"
typedef int ELEM;
#include "..\include\dlinkf.h" // Doubly-linked list nodes
#include "..\include\llist.h"
// This is poor style to include this code. The only reason I do it
// is because otherwise lprint.c would need to include the proper
// list header, which would require a separate lprint.c version for
// array-based and linked lists.
#include "..\include\lprint.c"
main()
{
list L1;
list L2(15);
list L3;
L2.append(10);
print(L2);
L2.append(20);
L2.append(15);
print(L2);
L1.setFirst();
L1.insert(39);
L1.next();
L1.insert(12);
L1.append(5);
L1.append(4);
L1.append(3);
L1.append(2);
L1.append(1);
print(L1);
L2.setFirst();
L2.find(20);
if (L2.isInList())
cout << "L2 curr: " << L2.currValue() << "\n";
else
cout << "Odd - what happened to 20?\n";
cout << "L1: "; print(L1);
cout << "Size: " << L1.length() << "\n";
cout << "L2: "; print(L2);
cout << "L3: "; print(L3);
L1.setFirst();
L1.find(20);
if (L1.isInList())
cout << "Odd - shouldn't find 20 in L1!\n";
else
cout << "20 not in L1.\n";
L1.setFirst();
L1.find(5);
if (L1.isInList())
cout << L1.currValue() << "\n";
L2.setFirst();
L2.find(20);
if(L2.isInList())
cout << L2.currValue() << "\n";
else
cout << "Didn't find it the first time\n";
L2.next();
L2.find(20);
if (L2.isInList())
cout << "Found it again!\n";
else
cout << "Only once in list.\n";
L1.setFirst();
cout << L1.currValue() << "\n";
L1.setValue(42);
print(L1);
L2.setFirst();
cout << "Removing: " << L2.remove() << "\n";
cout << "Removing: " << L2.remove() << "\n";
print(L2);
cout << "Size: " << L2.length() << "\n";
L2.clear(); print(L2);
cout << "Size: " << L2.length() << "\n";
L2.append(5); print(L2);
cout << "Start a new round.\n";
L2.clear();
print(L2);
L2.setFirst();
L2.insert(1);
print(L2);
L2.insert(2);
print(L2);
L2.setPos(2);
L2.insert(3);
print(L2);
cout << "That is all.\n";
cout << "-----------------------\n";
return(0);
}
⌨️ 快捷键说明
复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?