1.对任意输入的一组数据,建立一个递增有序的单链表
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define null = NULL;
typedef struct node
{
int data;
struct node* next;
}node, * LinkList;
enum error_type{ success, overflow, underflow, rangeerror, arrange_error};
class list {
public:
list();
int length()const;
error_type get_element(const int i, int & x) const; //按序号查找元素
node* locate(const int x)const; //按值查找元素
error_type insert(const int i, const int x); //插入元素
error_type delete_element(const int i); //删除元素
node* get_head() { return head; }; //读取链表表头指针
void create1(); //表头插入法
void create2(); //表尾插入法
void display(); //输出链表
void insert1(int x);
private:
int count;
node* head;
};
list::list() //初始化list
{
head = new node;
head->next = NULL;
count = 0;
}
int list::length()const //求长度的实现
{
node *p = head->next;
int n = 0; //计数过程,定义一个n用来计数
while (p != NULL)
{
n++;
p = p->next;
}
return n;
}
error_type list::get_element(const int i, int& x)const //按序号取元素的实现
{
node *p = head->next; int j = 1;
while (j != i && p != NULL)
{
p = p->next; j++;
}
if (p == NULL)
return rangeerror;
x = p->data;
return success;
}
node* list::locate(const int x)const { //按值查找元素的实现
node *p = head->next;
while (p != NULL) {
if (p->data == x)
return p;
else
p = p->next;
}
}
error_type list::insert(const int i, const int x) { //插入运算的实现
node *p = head;int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
if (i<1 || i>count + 1)
return arrange_error;
node *s = new node;
s->data = x;
s->next = p->next;
p->next = s;
count++;
return success;
}
error_type list::delete_element(const int i) //删除运算的实现
{
node *p = head; int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next; j++;
}
if (i < 1 || i > count) // if ( p == NULL ||p->next==NULL )
return rangeerror;
node *u = p->next;
p->next = u->next;
delete u;
count--;
return success;
}
void list::create1() { //表头插入法实现链表创建
int x;
cin >> x;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = head->next;
head->next = s;
cin >> x;
}
}
void list::create2() { //表位插入法实现链表创建
int x;
cin >> x;
node *rear = head;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
cin >> x;
}
}
void list::display() { //输出链表
node* p = head->next;
while (p != NULL) { cout << p->data << " "; p = p->next; }
cout << endl;
}
void list::insert1(int x) //增序插入数
{
node* u, * P;
P = head;
while (P->next != NULL && P->next->data < x) //搜索插入位置
P = P->next;
if (P->next == NULL || P->next->data > x)
{
u = new node;
u->data = x;
u->next = P->next; //插入新结点
P->next = u;
count++;
}
}
int main() {
list abc;
int x;
cout << "下面开始输入数字;(提示:输入0结束!)" << endl;
cin >> x;
while (x != 0) {
abc.insert1(x);
cin >> x;
}
abc.display();
return 0;
}
2.将单链表L中的奇数项和偶数项结点分解开,并分别连成一个单链表
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define null = NULL;
typedef struct node
{
int data;
struct node* next;
}node, * LinkList;
enum error_type{ success, overflow, underflow, rangeerror, arrange_error};
class list {
public:
list();
int length()const;
error_type get_element(const int i, int & x) const; //按序号查找元素
node* locate(const int x)const; //按值查找元素
error_type insert(const int i, const int x); //插入元素
error_type delete_element(const int i); //删除元素
node* get_head() { return head; }; //读取链表表头指针
void create1(); //表头插入法
void create2(); //表尾插入法
void display(); //输出链表
void create(list*& l); //创建一个单链表
void devide(list* L, list* L1, list* L2); //拆分线性表
private:
int count;
node* head;
node* next;
};
list::list() //初始化list
{
head = new node;
head->next = NULL;
count = 0;
}
int list::length()const //求长度的实现
{
node *p = head->next;
int n = 0; //计数过程,定义一个n用来计数
while (p != NULL)
{
n++;
p = p->next;
}
return n;
}
error_type list::get_element(const int i, int& x)const //按序号取元素的实现
{
node *p = head->next; int j = 1;
while (j != i && p != NULL)
{
p = p->next; j++;
}
if (p == NULL)
return rangeerror;
x = p->data;
return success;
}
node* list::locate(const int x)const { //按值查找元素的实现
node *p = head->next;
while (p != NULL) {
if (p->data == x)
return p;
else
p = p->next;
}
}
error_type list::insert(const int i, const int x) { //插入运算的实现
node *p = head;int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
if (i<1 || i>count + 1)
return arrange_error;
node *s = new node;
s->data = x;
s->next = p->next;
p->next = s;
count++;
return success;
}
error_type list::delete_element(const int i) //删除运算的实现
{
node *p = head; int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next; j++;
}
if (i < 1 || i > count) // if ( p == NULL ||p->next==NULL )
return rangeerror;
node *u = p->next;
p->next = u->next;
delete u;
count--;
return success;
}
void list::create1() { //表头插入法实现链表创建
int x;
cin >> x;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = head->next;
head->next = s;
cin >> x;
}
}
void list::create2() { //表位插入法实现链表创建
int x;
cin >> x;
node *rear = head;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
cin >> x;
}
}
void list::display() { //输出链表
node* p = head->next;
while (p != NULL) { cout << p->data << " "; p = p->next; }
cout << endl;
}
void list::devide(list* L, list* L1, list* L2)
{
int x, i;
for (i = 1; i < L->length() + 1; i++) {
L->get_element(i, x);
if (i % 2 != 0) {
int j = 1;
L1->insert(j, x);
}
else {
int j = 1;
L2->insert(j, x);
}
}
}
int main() {
list L, L1, L2;
L.create1();
cout << "链表为:" << endl;
L.display();
L.devide(&L,&L1,&L2);
cout << endl;
cout << "链表的奇数项为:" << endl;
L1.display();
cout<<endl;
cout << "链表的偶数项为:" << endl;
L2.display();
cout << endl;
return 0;
}
3.用递增有序的链表A、B表示两个集合,判断B是否是A的子集
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define null = NULL;
typedef struct node
{
int data;
struct node* next;
}node, * LinkList;
enum error_type{ success, overflow, underflow, rangeerror, arrange_error};
class list {
public:
list();
int length()const;
error_type get_element(const int i, int & x) const; //按序号查找元素
node* locate(const int x)const; //按值查找元素
error_type insert(const int i, const int x); //插入元素
error_type delete_element(const int i); //删除元素
node* get_head() { return head; }; //读取链表表头指针
void create1(); //表头插入法
void create2(); //表尾插入法
void display(); //输出链表
private:
int count;
node* head;
node* next;
};
list::list() //初始化list
{
head = new node;
head->next = NULL;
count = 0;
}
int list::length()const //求长度的实现
{
node *p = head->next;
int n = 0; //计数过程,定义一个n用来计数
while (p != NULL)
{
n++;
p = p->next;
}
return n;
}
error_type list::get_element(const int i, int& x)const //按序号取元素的实现
{
node *p = head->next; int j = 1;
while (j != i && p != NULL)
{
p = p->next; j++;
}
if (p == NULL)
return rangeerror;
x = p->data;
return success;
}
node* list::locate(const int x)const { //按值查找元素的实现
node *p = head->next;
while (p != NULL) {
if (p->data == x)
return p;
else
p = p->next;
}
}
error_type list::insert(const int i, const int x) { //插入运算的实现
node *p = head;int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
if (i<1 || i>count + 1)
return arrange_error;
node *s = new node;
s->data = x;
s->next = p->next;
p->next = s;
count++;
return success;
}
error_type list::delete_element(const int i) //删除运算的实现
{
node *p = head; int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next; j++;
}
if (i < 1 || i > count) // if ( p == NULL ||p->next==NULL )
return rangeerror;
node *u = p->next;
p->next = u->next;
delete u;
count--;
return success;
}
void list::create1() { //表头插入法实现链表创建
int x;
cin >> x;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = head->next;
head->next = s;
cin >> x;
}
}
void list::create2() { //表位插入法实现链表创建
int x;
cin >> x;
node *rear = head;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
cin >> x;
}
}
void list::display() { //输出链表
node* p = head->next;
while (p != NULL) { cout << p->data << " "; p = p->next; }
cout << endl;
}
int judge(list *L1,list *L2)
{
int x;
if (L1->length() < L2->length())
cout << "B不是A的子集"<<endl;
else
{
for (int i = 1; i < L2->length() + 1; i++)
{
L2->get_element(i, x);
x = L1->locate(x)->data;
if (x != -1) continue;
else
{
cout << "B不是A的子集" << endl;;
goto a;
}
}
cout << "B是A的子集" << endl;;
}
a:return 0;
}
int main() {
list L1, L2;
cout << "请输入L1:(以0结束)" << endl;
L1.create2();
cout << "请输入L2:(以0结束)" << endl;
L2.create2();
judge(&L1, &L2);
return 0;
}
4.用递增有序的链表A、B表示两个集合,设计算法求它们的并集。
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define null = NULL;
typedef struct node
{
int data;
struct node* next;
}node, * LinkList;
enum error_type{ success, overflow, underflow, rangeerror, arrange_error};
class list {
public:
list();
int length()const;
error_type get_element(const int i, int & x) const; //按序号查找元素
node* locate(const int x)const; //按值查找元素
error_type insert(const int i, const int x); //插入元素
error_type delete_element(const int i); //删除元素
node* get_head() { return head; }; //读取链表表头指针
void create1(); //表头插入法
void create2(); //表尾插入法
void display(); //输出链表
void minus_LinkList(list A, list B,list &C);
private:
int count;
node* head;
node* next;
};
list::list() //初始化list
{
head = new node;
head->next = NULL;
count = 0;
}
int list::length()const //求长度的实现
{
node *p = head->next;
int n = 0; //计数过程,定义一个n用来计数
while (p != NULL)
{
n++;
p = p->next;
}
return n;
}
error_type list::get_element(const int i, int& x)const //按序号取元素的实现
{
node *p = head->next; int j = 1;
while (j != i && p != NULL)
{
p = p->next; j++;
}
if (p == NULL)
return rangeerror;
x = p->data;
return success;
}
node* list::locate(const int x)const { //按值查找元素的实现
node *p = head->next;
while (p != NULL) {
if (p->data == x)
return p;
else
p = p->next;
}
}
error_type list::insert(const int i, const int x) { //插入运算的实现
node *p = head;int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
if (i<1 || i>count + 1)
return arrange_error;
node *s = new node;
s->data = x;
s->next = p->next;
p->next = s;
count++;
return success;
}
error_type list::delete_element(const int i) //删除运算的实现
{
node *p = head; int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next; j++;
}
if (i < 1 || i > count) // if ( p == NULL ||p->next==NULL )
return rangeerror;
node *u = p->next;
p->next = u->next;
delete u;
count--;
return success;
}
void list::create1() { //表头插入法实现链表创建
int x;
cin >> x;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = head->next;
head->next = s;
cin >> x;
}
}
void list::create2() { //表位插入法实现链表创建
int x;
cin >> x;
node *rear = head;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
cin >> x;
}
}
void list::display() { //输出链表
node* p = head->next;
while (p != NULL) { cout << p->data << " "; p = p->next; }
cout << endl;
}
void list:: minus_LinkList(list A,list B,list &C)
{
node* tempA = A.get_head()->next;
node* tempB = B.get_head()->next;
node* tempC = C.get_head();
int x;
while (tempA != NULL)
{
node* s = new node;
s->data = tempA->data;
tempC->next = s;
tempC = s;
tempC->next = NULL;
tempA = tempA->next;
}
tempC = C.get_head();
while (tempB != NULL)
{
x = tempB->data;
if (C.locate(x) == NULL)
{
node* s = new node;
s->data = x;
s->next = tempC->next;
tempC->next = s;
}
tempB = tempB->next;
}
}
int main() {
list A, B, C;
cout << "请输入L1:(以0结束)" << endl;
A.create2();
cout << "请输入L2:(以0结束)" << endl;
B.create2();
A.minus_LinkList(A, B, C);
C.display();
return 0;
}
5.设计算法判断单循环链表是否每个结点的值都是偶数。
#include<iostream>
#include<algorithm>
#include<string>
using namespace std;
#define null = NULL;
typedef struct node
{
int data;
struct node* next;
}node, * LinkList;
enum error_type{ success, overflow, underflow, rangeerror, arrange_error};
class list {
public:
list();
int length()const;
error_type get_element(const int i, int & x) const; //按序号查找元素
node* locate(const int x)const; //按值查找元素
error_type insert(const int i, const int x); //插入元素
error_type delete_element(const int i); //删除元素
node* get_head() { return head; }; //读取链表表头指针
void create1(); //表头插入法
void create2(); //表尾插入法
void display(); //输出链表
int loop(list L);
private:
int count;
node* head;
node* next;
};
list::list() //初始化list
{
head = new node;
head->next = NULL;
count = 0;
}
int list::length()const //求长度的实现
{
node *p = head->next;
int n = 0; //计数过程,定义一个n用来计数
while (p != NULL)
{
n++;
p = p->next;
}
return n;
}
error_type list::get_element(const int i, int& x)const //按序号取元素的实现
{
node *p = head->next; int j = 1;
while (j != i && p != NULL)
{
p = p->next; j++;
}
if (p == NULL)
return rangeerror;
x = p->data;
return success;
}
node* list::locate(const int x)const { //按值查找元素的实现
node *p = head->next;
while (p != NULL) {
if (p->data == x)
return p;
else
p = p->next;
}
}
error_type list::insert(const int i, const int x) { //插入运算的实现
node *p = head;int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next;
j++;
}
if (i<1 || i>count + 1)
return arrange_error;
node *s = new node;
s->data = x;
s->next = p->next;
p->next = s;
count++;
return success;
}
error_type list::delete_element(const int i) //删除运算的实现
{
node *p = head; int j = 0;
while (j != i - 1 && p != NULL) {
p = p->next; j++;
}
if (i < 1 || i > count) // if ( p == NULL ||p->next==NULL )
return rangeerror;
node *u = p->next;
p->next = u->next;
delete u;
count--;
return success;
}
void list::create1() { //表头插入法实现链表创建
int x;
cin >> x;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = head->next;
head->next = s;
cin >> x;
}
}
void list::create2() { //表位插入法实现链表创建
int x;
cin >> x;
node *rear = head;
while (x != 0) { //截止符号是0
count++;
node *s = new node;
s->data = x;
s->next = NULL;
rear->next = s;
rear = s;
cin >> x;
}
}
void list::display() { //输出链表
node* p = head->next;
while (p != NULL) { cout << p->data << " "; p = p->next; }
cout << endl;
}
int list::loop(list L) {
int x;
for (int i = 1; i < L.length() + 1; i++) {
L.get_element(i, x);
if (x % 2 != 0) {
cout << "链表不是每个结点的值都是偶数" << endl;;
goto k;
}
}
cout << "链表每个结点的值都是偶数" << endl;;
k: return 0;
}
int main() {
list L;
cout << "请输入L:(以0结束)" << endl;
L.create1();
L.loop(L);
return 0;
}