Tabla de secuencia:
#include <iostream>
using namespace std;
const int MAX_n = 1000 ;
class Seqlist
{
public :
Seqlist ( ) {
length = 0 ; }
Seqlist ( int ca[ ] , int b) ;
~ Seqlist ( ) {
}
int Length ( ) {
return length; }
int get ( int i) ;
int locate ( int x) ;
void insert ( int q, int m) ;
int S_delete ( int i) ;
void printlist ( ) ;
private :
int a[ MAX_n] ;
int length;
} ;
Seqlist:: Seqlist ( int ca[ ] , int b)
{
if ( b > MAX_n)
{
cout << "参数非法" << endl;
return ;
}
for ( int i = 0 ; i < b; i++ )
{
a[ i] = ca[ i] ;
}
length = b;
}
int Seqlist:: get ( int i)
{
return a[ i - 1 ] ;
}
int Seqlist:: locate ( int x)
{
for ( int i = 0 ; i < length; i++ )
{
if ( a[ i] == x)
return i + 1 ;
}
}
void Seqlist:: insert ( int q, int m)
{
if ( length == MAX_n)
{
cout << "上溢" << endl;
return ;
}
if ( q > length || q < 1 )
{
cout << "位置" << endl;
return ;
}
for ( int i = length; i >= q; i-- )
{
a[ i] = a[ i - 1 ] ;
}
length++ ;
a[ q - 1 ] = m;
}
int Seqlist:: S_delete ( int i)
{
if ( length == 0 )
{
cout << "下溢" << endl;
return 0 ;
}
if ( i< 1 || i> length)
{
cout << "位置" << endl;
return 0 ;
}
int q = a[ i - 1 ] ;
for ( int j = i - 1 ; j < length- 1 ; j++ )
{
a[ j] = a[ j + 1 ] ;
}
length-- ;
return q;
}
void Seqlist:: printlist ( )
{
for ( int i = 0 ; i < length; i++ )
{
cout << a[ i] << " " ;
}
cout << endl;
}
int main ( )
{
int a[ 10 ] = {
12 , 42 , 35 , 444 , 775 , 56 , 17 , 98 , 49 , 210 } ;
Seqlist q ( a, 10 ) ;
q. printlist ( ) ;
int c= q. get ( 4 ) ;
cout << "第四个元素是:" << c << endl;
q. insert ( 4 , 22 ) ;
q. printlist ( ) ;
int m= q. Length ( ) ;
cout << "length=" << m << endl;
q. S_delete ( 7 ) ;
int d= q. locate ( 12 ) ;
cout << "12的位置是:" << d << endl;
q. printlist ( ) ;
q. S_delete ( 0 ) ;
return 0 ;
}
Lista enlazada:
#include <iostream>
using namespace std;
struct node
{
int data;
node* next;
} ;
class LinkList
{
public :
LinkList ( ) ;
LinkList ( int a[ ] , int n) ;
~ LinkList ( ) ;
int length ( ) ;
int get ( int i) ;
int locate ( int x) ;
void insert ( int i, int x) ;
int idelete ( int i) ;
void printlist ( ) ;
private :
node* first;
} ;
LinkList:: LinkList ( )
{
first = new node;
first- > next = NULL ;
}
LinkList:: LinkList ( int a[ ] , int n)
{
first = new node;
node* s;
node* r= first;
for ( int i = 0 ; i < n; i++ )
{
s = new node;
s- > data = a[ i] ;
r- > next = s;
r = s;
}
r- > next = NULL ;
}
LinkList:: ~ LinkList ( )
{
while ( first != NULL )
{
node * p = first;
first= first- > next;
delete p;
}
}
int LinkList:: length ( )
{
int count = 0 ;
node* s;
s = first- > next;
while ( s!= NULL )
{
count++ ;
s = s- > next;
}
return count;
}
int LinkList:: get ( int i)
{
node* s;
s = first;
int count = 0 ;
while ( s!= NULL && count < i)
{
s = s- > next;
count++ ;
}
if ( s == NULL )
{
cout << "位置" << endl;
return 0 ;
}
return s- > data;
}
int LinkList:: locate ( int x)
{
int count = 1 ;
node* s;
s = first- > next;
while ( s != NULL )
{
if ( s- > data == x)
return count;
s = s- > next;
count++ ;
}
return 0 ;
}
void LinkList:: insert ( int i, int x)
{
int count = 0 ;
node* s;
s = first;
while ( s!= NULL && count < i - 1 )
{
s = s- > next;
count++ ;
}
if ( s == NULL )
cout << "位置" << endl;
else
{
node* p = new node;
p- > data = x;
p- > next = s- > next;
s- > next = p;
}
}
int LinkList:: idelete ( int i)
{
node* s = first;
int count = 0 ;
while ( s != NULL && count < i - 1 )
{
s = s- > next;
count++ ;
}
if ( s == NULL || s- > next== NULL )
{
cout << "位置" << endl;
return 0 ;
}
node* q = s- > next;
int x = q- > data;
s- > next = q- > next;
delete q;
return x;
}
void LinkList:: printlist ( )
{
node* p = first- > next;
while ( p != NULL )
{
cout << p- > data << " " ;
p = p- > next;
}
cout << endl;
}
int main ( )
{
int a[ 10 ] = {
11 , 15 , 62 , 37 , 48 , 59 , 12 , 32 , 56 , 49 } ;
LinkList q ( a, 10 ) ;
cout << "链式表元素:" ;
q. printlist ( ) ;
cout<< "第五个元素是:" << q. get ( 5 ) << endl;
cout<< "元素12在表中位置是:" << q. locate ( 12 ) << endl;
cout<< "length:" << q. length ( ) << endl;
q. insert ( 3 , 99 ) ;
cout << "插入一个元素后length:" << q. length ( ) << endl;
cout << "链式表元素:" ;
q. printlist ( ) ;
q. idelete ( 9 ) ;
cout << "删除第9个元素后链式表元素为:" ;
q. printlist ( ) ;
return 0 ;
}
Invertir los elementos de la matriz:
void SeqList< DataType> :: inversion ( )
{
int i = 0 , j = length - 1 ;
DataType temp;
while ( i < j)
{
temp = data[ i] ;
data[ i++ ] = data[ j] ;
data[ j-- ] = temp;
}
}
Invertir los elementos de la lista enlazada:
void SeqList< DataType> :: inversion ( )
{
Node< DataType> * p, * q;
p = first- > next;
q = p- > next;
while ( q != NULL )
{
p = q;
q = q- > next;
p- > next = first- > next;
first- > next = p;
}
}
Un algoritmo para insertar una tabla lineal en una lista enlazada individualmente sin nodo principal y un algoritmo para insertar una operación en una lista enlazada individualmente con un nodo principal.
void LinkList< DataType> :: insert ( int i, DataType x)
{
s= first;
int count= 0 ;
while ( s!= NULL && count< i- 1 )
{
s= s- > next;
count++ ;
}
if ( s== NULL )
{
cout<< "位置" << endl;
}
else
{
p= new node;
p- > data= x;
p- > next= s- > next;
s- > next= p;
}
}
void LinkList< DataType> :: insert ( int i, DataType x)
{
if ( i== 1 )
{
s= new node;
s- > data= x;
s- > next= head- > next;
head- > next= s;
}
else
{
int count= 1 ;
p= head;
while ( p!= NULL && count< i- 1 )
{
p= p- > next;
count++ ;
}
if ( p== NULL )
{
cout<< "位置" << endl;
}
else
{
s= new node;
s- > data= x;
s- > next= p- > next;
p- > next= s;
}
}
}