Самая интересная информация только у нас!
Работа со списками - Форум
Меню сайта

Форма входа

Поиск

Наш опрос
Оцените мой сайт
Всего ответов: 31

Статистика

Приветствую Вас, Гость · RSS 20.04.2024, 03:45

[ Новые сообщения · Участники · Правила форума · Поиск · RSS ]
  • Страница 1 из 1
  • 1
Модератор форума: DenzeL  
Форум » Мир IT технологий » Языки прграммирования » Работа со списками (C++)
Работа со списками
ХиМиКДата: Вторник, 26.10.2010, 20:56 | Сообщение # 1
Злобный циник
Группа: Администратор
Сообщений: 339
Статус:
//по заданному списку построить новый список, включив в него элементы в обратном порядке.

Code
#include <iostream>
#include <list>
#include <time.h>

using namespace std;

int main()

{
      setlocale (LC_ALL, "Russian");
         srand(time(0));
  list< int > L;
  list< int > ::iterator it;
int N=1, i=1;
while (N!=0)
{
printf("Введите %d-й элемент списка: ", i);
scanf("%d", &N);
if (N==0)
break;
L.push_back(N);
i++;
}
  for (it = L.begin(); it!=L.end(); ++it)  
  cout << *it <<" \n";
  cout << endl;
  L.reverse ();
  cout << "V obratnom poryadke : \n";  
  for (it = L.begin(); it!=L.end(); ++it)  
   cout << *it <<" \n";
  cout << endl;

  system ("pause");
}

//добавить новый элемент в непустой упорядоченный список так, чтобы сохранить его упорядоченность.

Code
#include <time.h>
#include <iostream>
#include <list>9:54 25.10.2010

using namespace std;

int main()
{
         setlocale (LC_ALL, "Russian");
         srand(time(0));
  list< int > L;
  list< int > ::iterator i;
  int N;
         printf ("Razmer spiska - ");
         scanf("%d", &N);

   for (int i = 0; i<N; i++)L.push_back(i);
  cout << "spisok : \n" ;
  for (i = L.begin(); i!=L.end(); ++i)
   cout << *i <<"\n";
  cout << endl;
  int M;
         printf ("vvedite 4islo - ");
         scanf("%d", &M);
   i=L.begin(); L.insert (i, M);
   cout << "Posle vstavki M v nashalo :\n";
    L.sort ();
     for (i = L.begin(); i!=L.end(); ++i)  
    cout << *i <<"\n";
    cout << endl;
    system ("pause");
  }

//удалить из непустого списка первый элемент

Code
#include <locale.h>
#include <stdlib.h>
#include <time.h>
#include <stdio.h>

struct node
{
         int cell;
         node *next;
};

int main()
{
         setlocale (LC_ALL, "Russian");
         srand(time(0));
         int i=0,n,c=0,x;
         node *first=new node;
         node *j;
         j=first;
         printf ("Введите размер списка - ");
         scanf("%d", &n);
for (i;i<n;i++)
         {
                 scanf("%d", &x);
                 j->cell=x;
                 j->next=new node;
                 j=j->next;
                 j->next=NULL;
         }
                         j=first;
                         first=first->next;
                         delete j;

         printf ("Список :\n");
         j=first;
         i=0;
while (j->next!=NULL)
         {
                 printf (" №%i - %i\n",i,j->cell);
                 i++;
                 j=j->next;
         };

         system ("Pause");
         return 0;
}

//построить список, включив в него по одному разу элементы, которые входят одновременно в оба списка L1 и L2.

Code
#include <time.h>
#include <iostream>
#include <list>

using namespace std;

int main()
{
      setlocale (LC_ALL, "Russian");
         srand(time(0));
  list< int > L1,L2,L3;
  list< int > ::iterator i;
  int N;
  int M;
     cout <<"Razmer spiska №1 - ";
        cin >> N;
   for (int i = 0; i<N; i++)L1.push_back(M=rand()%10);
  cout << "spisok №1 : \n" ;
  for (i = L1.begin(); i!=L1.end(); ++i)  
  cout << *i <<" \n";
  cout << endl;
      cout <<"Razmer spiska №2 - ";
        cin >> N;
   for (int i = 0; i<N; i++)L2.push_back(M=rand()%10);
   cout << "spisok №2 : \n" ;
  for (i = L2.begin(); i!=L2.end(); ++i)  
  cout << *i <<" \n";
  cout << endl;
  L1.splice (L1.begin(), L2);
  L1.sort();
  L1.unique ();
  cout << "spisok#3 : \n" ;
  for (i = L1.begin(); i!=L1.end(); ++i)  
  cout << *i <<" \n";
  cout << endl;
  system ("pause");
}

Пишем сюда и свои коды по теме "Списки"

 
ХиМиКДата: Четверг, 25.11.2010, 13:28 | Сообщение # 2
Злобный циник
Группа: Администратор
Сообщений: 339
Статус:
//добавить новый элемент в непустой упорядоченный список так, чтобы сохранить его упорядоченность. Без использования класса list

Code
#include <iostream>
#include <conio.h>

using std::cin;
using std::cout;

struct list
{
   int chislo;
   list *prev,*next;   
};

list *first_list=NULL;
list *last_list=NULL;
list *it1=NULL;
list *it2=NULL;
int tek=1;

void VNachalo()
{
   list *ischo = new list;
   first_list->prev = ischo;
   ischo->prev = NULL;
   ischo->next = first_list;
   first_list = ischo;
   ischo->chislo = tek;
}

void VKonec()
{
   list *ischo = new list;
   last_list->next = ischo;
   ischo->prev = last_list;
   ischo->next = NULL;
   last_list = ischo;
   ischo->chislo = tek;
}

void VSeredinu()
{
   it1 = it1->prev;
   it2 = it1->next;
   list *ischo = new list;
   it1->next = ischo;
   ischo->prev = it1;
   ischo->next = it2;
   it2->prev = ischo;
   ischo->chislo = tek;
}

int main()
{
   setlocale(LC_ALL, "Russian");
   cout<<"Вводите элементы списка. Для прекращения сего занятия введите 0:\n";
   while (tek!=0)
   {
    cin>>tek;
    if (tek==0)
     break;
    if (first_list==NULL)
    {
     first_list = new list;
     it2=it1=last_list=first_list;
     first_list->next = first_list->prev = last_list->next = last_list->prev = it1->prev = it1->next = it2->prev = it2->next = NULL;
     first_list->chislo = tek;
    }
    else
    {
     if (tek <= first_list->chislo)
      VNachalo();
     else
     {
      if (tek >= last_list->chislo)
       VKonec();
      else
      {
       it1=first_list;
       while (it1->chislo < tek  &&  it1->next != NULL)
        it1 = it1->next;
       VSeredinu();
      }
     }
    }
   }
   cout<<"Получившийся упорядоченный список: ";
   it1=first_list;
   while (it1->next != NULL)
   {
    cout<<it1->chislo<<", ";
    it1 = it1->next;
   }
   cout<<it1->chislo<<".";
   getch();
   cout<<"\nНапоследок введите ещё одно число: ";
   cin>>tek;
   if (tek <= first_list->chislo)
    VNachalo();
   else
   {
    if (tek >= last_list->chislo)
     VKonec();
    else
    {
     it1=first_list;
     while (it1->chislo < tek  && it1->next != NULL)
      it1 = it1->next;
     VSeredinu();
    }
   }
   cout<<"Получившийся упорядоченный список с новым числом: ";
   it1=first_list;
   while (it1->next != NULL)
   {
    cout<<it1->chislo<<", ";
    it1 = it1->next;
   }
   cout<<it1->chislo<<".";
   getch();
   return 0;
}
 
ХиМиКДата: Четверг, 25.11.2010, 13:31 | Сообщение # 3
Злобный циник
Группа: Администратор
Сообщений: 339
Статус:
//построить список, включив в него по одному разу элементы, которые входят одновременно в оба списка L1 и L2. Без использования класса list

Code
#include <iostream>
#include <conio.h>

using std::cout;
using std::cin;

struct list
{
  int chislo;
  list *prev,*next;  
};

list *L1_L2;
list *it2,*it3;
list *temp;

void Push_Elem()
{
  if (L1_L2==NULL)
   {
    L1_L2=new list;
    L1_L2->next=L1_L2->prev=NULL;
    L1_L2->chislo = it2->chislo;
    it3=L1_L2;
   }
   else
   {
    list *ischo3 = new list;
    ischo3->chislo = it2->chislo;
    ischo3->next = NULL;
    ischo3->prev = it3;
    it3->next = ischo3;
    it3=ischo3;
   }
}

void Delete_Elem()
{
  if (temp->next != NULL)
  {
   list *tempn = temp->next;
   tempn->prev = temp->prev;
  }
  list *tempp = temp->prev;
  tempp->next = temp->next;
  delete temp;
  temp = tempp;
}

int main()
{
  setlocale(LC_ALL, "Russian");
  list *first_list=NULL;
  list *it1;
  int tek=1;
  cout<<"Вводите элементы 1-го списка. Для прекращения сего занятия введите 0.\n";
  while (tek!=0)
  {
   cin>>tek;
   if (tek==0)
    break;
   if (first_list==NULL)
   {
    first_list=new list;
    first_list->next=first_list->prev=NULL;
    first_list->chislo = tek;
    it1=first_list;
   }
   else
   {
    list *ischo = new list;
    ischo->chislo = tek;
    ischo->next = NULL;
    ischo->prev = it1;
    it1->next = ischo;
    it1=ischo;
   }
  }
  list *sec_list=NULL;
  cout<<"Вводите элементы 2-го списка. Для прекращения сего занятия введите 0.\n";
  tek=1;
  while (tek!=0)
  {
   cin>>tek;
   if (tek==0)
    break;
   if (sec_list==NULL)
   {
    sec_list=new list;
    sec_list->next=sec_list->prev=NULL;
    sec_list->chislo = tek;
    it2=sec_list;
   }
   else
   {
    list *ischo2 = new list;
    ischo2->chislo = tek;
    ischo2->next = NULL;
    ischo2->prev = it2;
    it2->next = ischo2;
    it2=ischo2;
   }
  }
  for (it1=first_list; it1->next != NULL; it1 = it1->next)
  {
   for (it2=sec_list; it2->next != NULL; it2 = it2->next)
   {
    if (it1->chislo == it2->chislo)
    {
     Push_Elem();
     break;
    }
   }
   if (it1->chislo == it2->chislo)
    Push_Elem();
  }
  if (it1->chislo == it2->chislo)
   Push_Elem();
  cout<<"Список элементов, встречающихся одновременно в обоих списках (исключены повто-\nры): ";
  for (it3=L1_L2; it3 && it3->next != NULL; it3 = it3->next)
  {
   for (temp = it3->next; temp->next != NULL; temp = temp->next)
   {
    if (temp->next == NULL)
     break;
    if (temp->chislo == it3->chislo)
     Delete_Elem();
   }
   if (temp->chislo == it3->chislo)
    Delete_Elem();
  }
  it3=L1_L2;
  while (it3->next != NULL)
  {
   cout<<it3->chislo<<", ";
   it3 = it3->next;
  }
  cout<<it3->chislo<<".";
  getch();
  return 0;
}
 
ХиМиКДата: Четверг, 25.11.2010, 13:33 | Сообщение # 4
Злобный циник
Группа: Администратор
Сообщений: 339
Статус:
Определить, есть ли в списке два одинаковых соседних элемента.Без использования класса list

Code

#include <locale.h>
#include <stdlib.h>
#include <stdio.h>
#include <iostream>

using namespace std;

struct node
{
   int cell;
   node *next;
   node *prev;
};
node *f;
int main()
{
         setlocale (LC_ALL, "Russian" );
         int i=0,n,c=0,x;
         node *Begin = new node;
         Begin->prev = NULL ;
         node *End = NULL;
         node *j;
         j=Begin;
         printf ("Введите размер списка - " );
         scanf("%d", &n);
         scanf("%d", &x);
         Begin->cell = x ;
         for (;i<(n-1);i++)
         {
             scanf("%d", &x);
             j->next=new node;
             j->next->prev = j ;
             j=j->next;
             j->cell=x;
             j->next=NULL;
         }
         End = j ;
         printf ("Список :\n" );
         j=Begin;
         i=0;
         while (1)
         {
         printf ("%i\n",j->cell);
             i++;
             if ( j->next != NULL )
             j=j->next;
             else
             break ;
         }
         int ass=0;
         j=Begin;
         while ( j->next != NULL )
         {
             if ( j->cell==j->next->cell )
             {
                 ass++;
             }
             j=j->next;
         }

         if (ass>0)
             cout<<"true\n";
         if (ass==0)
             cout<<"false\n";
         system ("Pause") ;
         return 0;
}
 
Форум » Мир IT технологий » Языки прграммирования » Работа со списками (C++)
  • Страница 1 из 1
  • 1
Поиск:


Copyright MyCorp © 2024