Форум программистов
 

Восстановите пароль или Зарегистрируйтесь на форуме, о проблемах и с заказом рекламы пишите сюда - alarforum@yandex.ru, проверяйте папку спам!

Вернуться   Форум программистов > IT форум > Помощь студентам
Регистрация

Восстановить пароль

Купить рекламу на форуме - 42 тыс руб за месяц

Ответ
 
Опции темы Поиск в этой теме
Старый 08.10.2014, 20:09   #1
Aerowalk
Пользователь
 
Регистрация: 27.10.2008
Сообщений: 15
По умолчанию Удаление элемента списка

Привет, всем. Очень нужна помощь в проблеме удаления элемента (или хотя бы исключения его из списка). Удаляет строго 1 раз из любого места списка, затем при повторном программа вылетает. Просто, уже столько времени убил на безрезультатные попытки что-либо поменять. Пробовал запускать на разных компиляторах. Смотрел иные коды. Хелп, плиз!!! Случай 4.
MainClass.cpp
Код:
#include "MainClass.h"
#include <conio.h>
#include <iostream>
#include <windows.h>
#pragma warning (disable:4996)

MainClass* MainClass::current = NULL;	
MainClass* MainClass::head = NULL;
MainClass* MainClass::prev = NULL;
MainClass* MainClass::tail = NULL;
int MainClass::counter = 0;

MainClass::MainClass(){
	key = new char[20];
}
MainClass::~MainClass(){
	delete[] key;
}

int MainClass::getNumber(){
	return number;
}
void MainClass::setNumber(int number){
	this->number=number;
}
char* MainClass::getKey(){
	return key;
}
void MainClass::setKey(char* key){
	strcpy(this->key, key);
}
int MainClass::getIntKey(){
	return intKey;
}
void MainClass::setIntKey(int intKey){
	this->intKey = intKey;
}
MainClass* MainClass::getNext(){
	return next;
}
void MainClass::setNext(MainClass* next){
	this->next = next;
}

void MainClass::menu(){
		std::cout << "      MENU";
		std::cout << "\n\n1   - show list";
		std::cout << "\n2   - search by number";
		std::cout << "\n3   - add new element before max intKey";
		std::cout << "\n4   - delete element with max intKey";
		std::cout << "\nESC - Exit";
}
void MainClass::main(){
	//int n;
	//std::cout << "List's length? ";
	//std::cin >> n;
	createList(/*n*/);
	char keyMenu = '0';
	char* key = new char[20];
	int intKey = 0;
	int maxIntKey = INT_MIN;
	MainClass* temp = new MainClass;
	MainClass* temp2 = new MainClass;
	MainClass* lastEven = new MainClass;
	while (keyMenu != 27)
	{
		system("cls");
		menu();
		keyMenu = _getch();

		switch (keyMenu)
		{
		case '1':
			current = head;
			std::cout << "\n\nkey:\tintKey:\tnumber:";
			while (!current == NULL)
			{
				std::cout << "\n" << current->getKey() << "\t";
				std::cout << current->getIntKey() << "\t";
				std::cout << current->getNumber();
				current = current->getNext();
			}
			_getch();
			break;
		case '2':
			int searchNumber;
			std::cout << "\n\nnumber is?\t";
			std::cin >> searchNumber;
			std::cout << "\nkey:\tintKey:\tnumber:\n";

			current = head;
			while (!current == NULL)
			{
				if (current->getNumber() == searchNumber){
					std::cout << current->getKey() << "\t";
					std::cout << current->getIntKey() << "\t";
					std::cout << current->getNumber();
				}
				current = current->getNext();
			}
			_getch();
			break;
Aerowalk вне форума Ответить с цитированием
Старый 08.10.2014, 20:09   #2
Aerowalk
Пользователь
 
Регистрация: 27.10.2008
Сообщений: 15
По умолчанию Удаление элемента списка

Код:
		case '3':
			current = head;
			while (!current == NULL){
				if (maxIntKey < current->getIntKey())
					maxIntKey = current->getIntKey();
				current = current->getNext();
			}
			//current = new MainClass;
			if (head->getIntKey() == maxIntKey){
				std::cout << "\nnew charKey is?\t";
				std::cin >> key;
				std::cout << "new intKey is?\t";
				std::cin >> intKey;
				current->setKey(key);
				current->setIntKey(intKey);
				current->setNumber(counter);
				counter++;
				current->setNext(head);
				head = current;
			}
			else{
				MainClass* additional = new MainClass;
				current = head;
				while (current)
				{
					if (current->getNext()->getIntKey() == maxIntKey){
						std::cout << "\nnew charKey is?\t";
						std::cin >> key;
						std::cout << "new intKey is?\t";
						std::cin >> intKey;
						additional->setKey(key);
						additional->setIntKey(intKey);
						additional->setNumber(counter);
						counter++;
						additional->setNext(current->getNext());
						current->setNext(additional);
						break;
					}
					current = current->getNext();
				}
			}
			_getch();
			break;
		case '4':
//_____________________________________________________________________________________________________________________________________
			/*
			current = head;
			while (!current == NULL){
			if (maxIntKey < current->getIntKey())
			maxIntKey = current->getIntKey();
			current = current->getNext();
			}
			if (head->getIntKey() == maxIntKey){
			current = head;
			head = NULL;
			//delete head;
			if (current->getNext()!=NULL)
			head = current->getNext();
			}
			else{
			current = head;
			while (current)
			{
			if (current->getNext()->getIntKey() == maxIntKey){
			//current->getNext() = NULL;
			current->setNext(current->getNext()->getNext());
			break;
			}
			current = current->getNext();
			}
			}*/
			
			
			
			current = head;
			while (current){
				if (maxIntKey < current->getIntKey()){
					maxIntKey = current->getIntKey();
					temp = current;
				}
				current = current->getNext();
			}	//current в итоге здесь равен NULL
			for (current = head; current->getNext() != NULL; current = current->getNext()){}
			tail = current;			//current в итоге здесь равен последнему элементу
			if (temp == head){
				current = head;
				head = current->getNext();
				
				delete current;
			}
			else if(temp == tail){
				for (current = head; current->getNext()->getNext() != NULL; current = current->getNext()){}
				current->setNext(NULL);
				/*или 
				for (current = head; current->getNext()->getNext() != NULL; current = current->getNext()){}
				temp2=current;
				current=current->getNext();
				delete current;
				current=temp2;
				current->setNext(NULL);
				*/
			}
			else {
				for (current = head; current->getNext() != temp; current = current->getNext()){}
				//temp2 = current->getNext();
				current->setNext(temp->getNext());
				//current = temp2;
				//delete current;
			}
			break;
//______________________________________________________________________________________________________________________________________
		}
	}
}
void MainClass::createList(/*int n*/){
	/*current = new MainClass;

	std::cout<< "Key? ";  //ввод первого эл-та
	std::cin >> key;
	current->setKey(key);
	current->setNumber(0);
	current->setHead(current);

	for (int i = 0; i < n - 1; i++)
	{
		current->setNext(new MainClass);
		current->current->getNext();
		std::cout << "Key? ";
		std::cin >> key;
		current->setKey(key);
		current->setNumber(i + 1);
	}
	current->setNext(NULL);
	
	//tail = current;*/          
	char* key = new char[20];
	int intKey;      
	bool first = true;

	for (;;){
		std::cout << "charKey is?\t";
		std::cin >> key;
		if (!strcmp(key, "q")){
			break;
		}
		std::cout << "intKey is?\t";
		std::cin >> intKey;
		current = new MainClass;
		current->setKey(key);
		current->setIntKey(intKey);
		current->setNumber(counter);
		if (first == true)
		{
			head = current;
		}
		if (prev != NULL)
			prev->setNext(current);
		prev = current;

		first = false;
		counter++;
	}
}
MainClass.h
Код:
class MainClass{        
	int number;
	int intKey;
	char* key;
	MainClass* next;
public:
	static MainClass* current;
	static MainClass* head;
	static MainClass* tail;
	static MainClass* prev;
	static int counter;

	MainClass();
	~MainClass();
	int getNumber();
	void setNumber(int number);
	char* getKey();
	void setKey(char* key);
	int getIntKey();
	void setIntKey(int intKey);
	MainClass* getNext();
	void setNext(MainClass* next);

	void menu();
	void main();
	void createList(/*int n*/);
};
start.cpp
Код:
#include "MainClass.h"
#include <conio.h>
#include <iostream>
void main()
{
	MainClass* mainClassObject = new MainClass;
	mainClassObject->main();
}
Aerowalk вне форума Ответить с цитированием
Ответ


Купить рекламу на форуме - 42 тыс руб за месяц



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
c++ удаление элемента списка NinjaNoob Помощь студентам 1 28.01.2013 04:01
Удаление элемента списка igoldyrev Помощь студентам 2 06.03.2011 14:24
Удаление элемента из списка Ghost_gg Паскаль, Turbo Pascal, PascalABC.NET 2 30.05.2010 20:43
Удаление последнего элемента из списка и реверс этого списка. Goose Общие вопросы C/C++ 8 16.05.2010 16:12
удаление элемента из списка yagluboko Помощь студентам 1 10.04.2010 14:54