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

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

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

Восстановить пароль
Повторная активизация e-mail

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

Ответ
 
Опции темы Поиск в этой теме
Старый 26.12.2013, 18:27   #1
*Fantom*
Пользователь
 
Регистрация: 28.05.2013
Сообщений: 32
По умолчанию сортировка связного списка c++

Нужно добавить функцию которая бы сортировала список.
Код:
#include <stdio.h>
#include<conio.h>
#include <stdlib.h>
#include <time.h>
#include<iostream>
#include<cstdlib>
#define n 10
using namespace std;
struct Tlist{
	int inf;
	Tlist *next,*prev;};
int x,i;
class Clist{
	Tlist *head,*end,*curr;
public:
	Clist(){
		head=end=curr=NULL;}
		void add(int x);
		void showl();
		void showr();
		void sort();
		int* peres(Clist list1, Clist list2);
		int search(int x);
		~Clist();};
	void Clist:: add(int x){
		Tlist *temp;
		temp=new Tlist;
		temp->next=NULL;
		temp->inf=x;
		if(head==NULL){
			temp->prev=NULL;
			head=temp;
			end=temp;}
		else{
		    end->next=temp;
			temp->prev=end;
			end=temp;}}
	void Clist:: showl(){
		cout<<"vvuvod s leva\n";
		Tlist *temp;
		temp=head;
		while(temp!=NULL){
			cout<<temp->inf<<' ';
			temp=temp->next;}
	cout<<'\n';}
		void Clist:: showr(){
		cout<<"vvuvod s sprava \n";
		Tlist *temp;
		temp=end;
		while(temp!=NULL){
			cout<<temp->inf<<' ';
			temp=temp->prev;}
	cout<<'\n';}
		int Clist::search(int x){
	Tlist *temp;
	temp = head;
	while (temp != NULL){
		if (temp->inf == x){
			return 1;}
		temp = temp->next;}
	return 0;}
		int* Clist::peres (Clist list1, Clist list2){
			int *B,*C;
	list1.curr = list1.head;
	cout<<"Peresechenie \n";
	B=new int[n];
	C=B;
	while (list1.curr){
		if (list2.search(list1.curr->inf) == 1) {*B=list1.curr->inf; B++; }
		list1.curr = list1.curr->next; }	
	return C;}		
	Clist:: ~Clist(){
		Tlist *temp;
		while(end){
			temp=end->prev;
			delete end;
			end=temp;}}		
		void main(){
			Clist lst1,lst2,lst3;
			int k,l;
			int* A;
			l=400;
			for(k=100;k<500;k++){
				if((k%6)==0){
					x=k;
					lst1.add(x);}}
			for(l=400;l>0;l--){
				if(l%5==0){x=l;
            lst2.add(x);} }
			lst1.showl();
			lst2.showl();
			A=lst3.peres( lst1, lst2);
			for(int i=0;i<n;i++){
            lst3.add(*(A+i));			 }
			lst3.showl();
		getch();
		}
*Fantom* вне форума Ответить с цитированием
Ответ


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



Похожие темы
Тема Автор Раздел Ответов Последнее сообщение
Создание связного списка на Си zx11 Общие вопросы C/C++ 9 17.03.2014 00:54
Сортировка связного списка по алфавиту Євгеній Бєлік C/C++ Базы данных 0 02.12.2013 15:32
обращениe к члену связного списка soad33 Помощь студентам 0 03.11.2011 22:04
сортировка узлов связного списка pavelstraut Общие вопросы C/C++ 5 28.07.2009 23:27