סדנא תרגיל 5 שאלה 1

benny++C, נכתב ע"י Benny, סדנא ב++C, תרגילים בסדנא ב++CLeave a Comment

תכנית ראשית


/******************************
targil: 5/2
lecturer: david Cohen
discription: linkd list main operations 

*******************************/

#include <iostream>
#include "List.h"
using namespace std;

int main()
{
	List lst;
	int choice,val;
	cout<<"enter the list values\n";
	cin>>lst;
	cout<<"choose 0-2\n";
	cin>>choice;
	while (choice)
	{
		switch (choice)
		{
		case 1:cout<<"enter a value to insert\n";
			cin>>val;
			lst.insert(val);
			break;
		case 2:cout<<"enter a value to remove\n";
			cin>>val;
			try{
				lst.remove(val);
			}
			catch(char * msg)
			{
				cout<<msg<<endl;
			}
			break;
		default:cout<<"ERROR\n";
		}
		cout<<lst<<endl;
		cout<<"choose 0-2\n";
		cin>>choice;
	}
	//system ("pause");
	return 0;
}


/********OUTPUT*************

enter the list values
2 4 6 8  10 10
choose 0-2
1
enter a value to insert
1
1 2 4 6 8 10
choose 0-2
2
enter a value to remove
2
1 4 6 8 10
choose 0-2
1
enter a value to insert
0
0 1 4 6 8 10
choose 0-2
2
enter a value to remove
10
0 1 4 6 8
choose 0-2

*******************************/

המחלקה List

#pragma once
#include <iostream>
using namespace std;


class List
{
	private:
	
	class Link
	{
	public:
		// constructor
		Link(int linkValue, Link *nextPtr);
		Link (const Link &);
		// data areas
		int value;
		Link * next;
	};	

	public:
	Link* head;    //  prt to first elemant on the list

	// constructors
	List();
	List(const List&);
	~List();
	
	// operations

	void print() const;
	void operator=(const List& other);
	void clear();
	void setCin(int);
	friend istream& operator>>(istream&,  List&);
	friend ostream& operator<<(ostream&, const List&);

	void insert (int key);
	void remove (int key);


};

מימוש המחלקה List

#include "List.h"
//------------------------------------------------
//  class Link implementation
//------------------------------------------------
List::Link::Link( int val, Link* nxt) : value(val), next(nxt)   {}

List::Link::Link(const Link& source) : value(source.value),next(source.next)  {}
 
//--------------------------------------------
//  class List implementation
//--------------------------------------------

List::List(): head(NULL)
{
	//cout << "CTOR\n";
}

// Copy constructor
List::List(const List &l) 
{
	cout << "Copy CTOR\n";
	Link *src, *trg;
	if(l.head == NULL)
		head = NULL;
	else
	{  // copy the list
		head = new Link((l.head)->value, NULL);
		src = l.head;
		trg = head;
		while(src->next != NULL)
		{
			trg->next = new Link((src->next)->value, NULL);
			src = src->next;
			trg = trg->next;
		}
	}
}

void List::operator=(const List& l)
{
	//cout << "ASSIGNMENT OPERATOR\n";

	if(&l != this)                          // prevent self hasama
	{
		this->clear();
		Link *src, *trg;
		if(l.head == NULL)
			head = NULL;
		else
		{  // copy the list
			head = new Link((l.head)->value, NULL);
			src = l.head;
			trg = head;
			while(src->next != NULL)
			{
				trg->next = new Link((src->next)->value, NULL);
				src = src->next;
				trg = trg->next;
			}
		}
	}
}





// Destructor
List::~List()
{
	//cout << "DTOR: ";
	//print();
	clear();
}

void List::clear()
{
	// empty all elements from the List
	Link* next;
	for (Link *p = head; p != NULL; p = next)
	{
		// delete the element pointed to by p
		next = p->next;
		p->next = NULL;
		delete p;
	}
	// mark that the List contains no elements
	head=NULL;
}


void List::print() const
{
	
	for (Link* p = head; p != NULL ; p = p->next)
		cout << p->value <<" "; 
}



void List::setCin(int val)
{
	if (head==NULL)
	{
		head = new Link(val,head);
	}
	else
	{
		Link*p=head;
		while (p->next!=NULL)
		{
			p=p->next;
		}
		p->next=new Link(val,p->next);
	}
}


void List:: insert (int key)
{
	Link*p=head;     //pointer to the head of the list

	if (key<p->value)     // if key is small from the first one (samllet in the all list)
	{
		//cout<<"put in the begining\n";

		head=new Link(key,head);

		return;
	}

	while (p->next!=NULL)
		{

			if (key>p->value&&key<(p->next)->value)
			{
				//cout<<"put in the middle\n";
				p->next=new Link(key,p->next);
				return;
			}

			p=p->next;
		}

	while (p->next!=NULL)
		{

			p=p->next;
		}

	if (key>p->value)
			{
				//cout<<"put in the end\n";

				p->next=new Link(key,p->next);

				return;
			}

}


 void List::remove (int key)
 {

	 bool flag = false;       //  to know if throw exeption is necessary

	Link*p=head;

	if (key==p->value)        //if the key (the element to remove) is the first one on the list
	{
		head=head->next;      // promote the head to the next element

		flag=true;            // Match found so dont throw exeption
		return;               // exit now !!
		
	}


	while (p->next!=NULL)
	{
		if (key==(p->next)->value)     // compare the key to the (p->next)->value is nessesery for deleting 
		{                              // .. becauze the p pointer need to be jump (skip) on the deleting element
			p->next=(p->next)->next;

			flag=true;     //Match found so dont throw exeption

			return;
		}

		p=p->next;
	}
	
	 // in order thae the key(the element to remove) us the last element on the list

	while (p->next!=NULL)
		{

			p=p->next;           // go to the end of the list ...
		}

	if (key==p->value)
		{
			

			p->next=NULL;        // delete the last one

			flag=true;    //Match found so dont throw exeption

			return;
		}
	
	if (!flag)     //Match NOT found so throw exeption
	{
		throw "value not found";
	}

 }








istream& operator>>(istream& in,  List& list)
{
	int val1,val2;    // initialize to values to compare and only if the second value is bigger..
	in >> val1;       //.. then the first one continue to get input
	
	list.setCin(val1);
	
	in >> val2;
	

	while (val2>val1)    // if the next element > from the erlier
	{
			list.setCin(val2);
			
			val1=val2;
			in >> val2;
	}

	return in;
}


ostream& operator<<(ostream& out, const List& list)
{
	list.print();            // go to the print function
	return out;
	
}

כתיבת תגובה

האימייל לא יוצג באתר. שדות החובה מסומנים *