Void main(){
polin *nach, *nach1, *nach2;
int n, m;
cout<<"Enter n:";
cin>>n;
cout<<"Enter m:";
cin>>m;
creat(nach, n);
creat(nach1, m);
pokaz(nach);
pokaz(nach1);
proiz(nach1, nach, nach2);
prived(nach2);
upor(nach2);
pokaz(nach2);
}
Создать и показать полином.
#include <iostream>
using namespace std;
struct polinom{
double koef;
int step;
polinom *next;
};
void creat_polinom(polinom *&nach, int n){
polinom *p;
nach=NULL;
int i;
for(i=0;i<n;i++){
p=new polinom;
cin>>p->koef;
cin>>p->step;
p->next=nach;
nach=p;
}}
void pokaz_polinom(polinom *nach){
polinom *p;
cout<<"polinom="<<endl;
p=nach;
while(p!=NULL){
cout<<p->koef<<"*x^"<<p->step<<"+";
p=p->next;
}}
Void main(){
polinom *nach1;
int n;
cout<<"n=";
cin>>n;
creat_polinom(nach1, n);
pokaz_polinom(nach1);
}
Произведение всех отрицательных чисел списка.
#include <iostream.h>
struct sp {int data; sp * next;};
void create_sp ( sp * &nach, int n){
sp *p;
nach=NULL;
cout<<"soisok=";
for (int i=1; i<=n; i++){
p = new sp;
cin>> p->data;
p->next = nach;
nach = p;}}
void pokaz_sp (sp * nach){
sp * p;
p = nach;
while (p != NULL){
cout<<p->data<<' ';
p = p->next;}}
int proiz_otr (sp * nach){
sp * p;
int s;
p = nach;
s = 1;
while (p != NULL){
if (p->data < 0)
s=s*p->data;
p = p->next;}
return s;
}
void main(int argc, char* argv[]){
int n;
sp * nach1;
cout << "n=";
cin >> n;
create_sp (nach1, n);
pokaz_sp (nach1);
cout<<endl;
cout<<"proiz.otricat.chisel = "<<proiz_otr (nach1)<<endl;
}
Количество вхождений х в список.
#include <fstream>
#include <iostream>
using namespace std;
struct sp
{
int data;
sp* next;
};
void pokaz_sp(sp* nach)
{
sp* p;
cout<<"elementi spiska: ";
p=nach;
while(p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<"\n";
}
void vx(sp*nach,int x){
int i=0;
sp*p=nach;
while(p!=NULL)
{
if(p->data==x)
i++;
p=p->next;
}
if(i==0)
cout<<"Chilo ne vxodit\n";
else cout<<"Chislo vxogdenii="<<i<<"\n";
}
Void main()
{
sp *nach1,*p;
nach1=NULL;
ifstream in("1.txt");
while(!in.eof())
{
p=new sp;
in>>p->data;
p->next=nach1;
nach1=p;
}
pokaz_sp(nach1);
int c;
cout<<"c=";
cin>>c;
vx(nach1,c);
}
Из двух упорядоченных списков сделать один.
#include <iostream.h>
struct sp
{
int data;
sp *next;
};
void creat_sp(sp *&nach, int n)
{
sp *p;
nach=NULL;
int i;
for (i=1; i<=n; i++)
{
p=new sp;
cin>>p->data;
p->next=nach;
nach=p;
}
}
void pokaz_sp(sp *nach)
{
sp *p=nach;
cout<<"spisok=";
while (p!=NULL)
{
cout<<p->data<<" ";
p=p->next;
}
cout<<endl;
}
void slit(sp *nach1,sp * nach2,sp *&nach)
{
sp * p= nach1, *q=nach2;
sp *t;
nach = NULL;
while ((p!=NULL)&&(q!=NULL))
{
if(p->data<q->data)
{
t=new sp;
t->data=p->data;
t->next=nach;
nach=t;
p=p->next;
}
else
{
t=new sp;
t->data=q->data;
t->next=nach;
nach=t;
q=q->next;
}
}
while(p!=NULL)
{
t=new sp;
t->data=p->data;
t->next=nach;
nach=t;
p=p->next;
}
while (q!=NULL)
{
t=new sp;
t->data=q->data;
t->next=nach;
nach=t;
q=q->next;
}
}
void sort(sp *nach){
sp *p;
bool l=false;
while(!l){
l=true;
p=nach;
while(p->next!=NULL){
if(p->data>(p->next)->data){
l=false;
int t=p->data;
p->data=(p->next)->data;
(p->next)->data=t;
}
p=p->next;
}}}
int main ()
{
sp *nach1, *nach2, *nach;
int n;
int m;
cout<<"n:";
cin>>n;
cout<<"m:";
cin>>m;
creat_sp(nach1, n);
pokaz_sp(nach1);
creat_sp(nach2, m);
pokaz_sp(nach2);
slit(nach1, nach2, nach);
sort(nach);
pokaz_sp(nach);
return 0;
}
