Kanjut SHELL
Server IP : 172.16.15.8  /  Your IP : 3.139.234.124
Web Server : Apache
System : Linux zeus.vwu.edu 4.18.0-553.27.1.el8_10.x86_64 #1 SMP Wed Nov 6 14:29:02 UTC 2024 x86_64
User : apache ( 48)
PHP Version : 7.2.24
Disable Function : NONE
MySQL : OFF  |  cURL : ON  |  WGET : ON  |  Perl : ON  |  Python : ON
Directory (0705) :  /home/vnlaughlin/../cchansen/

[  Home  ][  C0mmand  ][  Upload File  ]

Current File : /home/vnlaughlin/../cchansen/list1.cpp
//
//	List ADT
//
//	Use C++ class to implement List ADT
//
//	p.138
//

#include <iostream>

using namespace std;

const int MAX = 50;

class List
{	int length;	// # of items in the list
	int data[MAX];
public:
	List()	// empty list
	{	length	= 0;	}
	void Insert( int newItem )
	{	
		data[length] = newItem;	
		length ++;
	}
	void Print() const
	{	for(int i=0; i<length; i++ )
			cout << data[i] << " ";
		cout << endl;
	}	
	// Seq. search -
	bool IsPresent(int item) const
	{	bool done = false;
		for(int k=0; k<length && !done; k++)
			if( data[k] == item )
				done = true;
		return done;
	}
	void SelSort();
};

void List :: SelSort ( ) 
{	int temp;	// for swapping 
	int passCount;
	int minIndex;	// hold the index of the minimum
	// total (length - 1) passes
	for ( passCount=0; passCount<length-1; passCount ++ )
	{	// search the smallest from present location
		minIndex = passCount;  
		// compare from next item
		for ( int k=passCount+1; k<length; k++ )
		{	 if ( data[minIndex] > data[k] )
				minIndex = k;
		}
	// find the minIndex, then swap it with the starting index
		temp = data[minIndex];
		data[minIndex] = data[passCount];
		data[passCount] = temp;
	}
}


int main()
{
	List z;
/*
	z.Insert(12);
	z.Insert(7);
	z.Insert(14);
*/
	// Q: create a list by inputting items from k.b.?
	// p.139

	int temp;
	cin >> temp;	// 12 7 14
	while(cin)	// EOF
	{	z.Insert(temp);
		cin >> temp;
	}

	// Q: 77 in  the list?
	if( z.IsPresent(77) )
		cout << "77 is in the list.\n\n";
	else	cout << "77 not in the list.\n\n";
	
	cout << "The old list is: ";
	z.Print();

	// do the sorting
	z.SelSort();

	cout << "\n\nThe sorted list is: ";
	z.Print();

	cout << "\n\nDone.\n\n";
	return 0;
}





Stv3n404 - 2023