//по заданному списку построить новый список, включив в него элементы в обратном порядке.
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");
}
Пишем сюда и свои коды по теме "Списки"