TheAmazing
Новичок
- Регистрация
- 3 Янв 2014
- Сообщения
- 7
- Баллы
- 1
		Код:
	
	#include <iostream>
#include <conio.h>
#include <cmath>
#include <stdlib.h>
using namespace std;
struct A
{
	int key;
};
struct List
{
	A a;
	List *next;
};
void Print(List *b)
{
	List *print = b;
	while(print)
	{
		cout << print->a.key << " -> ";
		print = print->next;
	}
	cout << "NULL\n";
};
void Init(List **begin)
{
		(*begin) = new List;
	
	A a[5] = {43,56,78,100,511};
	(*begin)->a.key = 20;
	(*begin)->next = NULL;
    List *end = (*begin);
	for(int i(0); i<5; i++)
	{
		end->next = new List;
		end = end->next;
		end->a = a[i];
		end->next = NULL;
	
	}
};
void Add_begin(List **begin,const A &a)
{
	List *t = new List;
	t->a = a;
	t->next = *begin; 
	*begin = t;
};
void Insert(List **begin, const A &a)
{
	List *ins = new List;
	ins->a = a;
	if(*begin == NULL)
	{
		ins->next = NULL;
		*begin = ins;
		return;
	}
	List *t = *begin;
	if(t->a.key > ins->a.key)
	{
		ins->next = t;
		*begin = ins;
		return;
	
	}
	List *t1 = t->next;
	while(t1)
	{
		if(t1->a.key < t->a.key && ins->a.key <= t1->a.key)
		{
			t->next = ins;
			ins->next = t1;
			return;
		
		}
		t = t1;
		t1 = t1->next;
	}
	t->next = ins;
	ins->next = NULL;
}
int main()
{
	setlocale (LC_ALL, "Russian");
	List* begin = NULL;
	A a={23};
	
	Init(&begin);
	Print(begin);
	Insert(&begin, a);
	Print(begin);
	_getch();
	return 0;
}"а" должна в значение 23 должна встать между 20 и 43, но это не выходит. 23 встает в конец списка. если поставить вместо 23, например 44, то тоже самое произойдет с 44.
ошибка у меня какая-то в функции Insert. Помогите исправить
 
				 
 
		 
 
		