#include<iostream>
using namespace std;
/*typedef struct student {
char name[6];
int xuehao;
}student;
*/
typedef struct LNode {
int date;
struct LNode* next;
}LNode, *LinkList;
void JianZao(LinkList& L); // 建造一个空链表
using namespace std;
/*typedef struct student {
char name[6];
int xuehao;
}student;
*/
typedef struct LNode {
int date;
struct LNode* next;
}LNode, *LinkList;
void JianZao(LinkList& L); // 建造一个空链表
// 建造一个空链表
void JianZao(LinkList &L) {
L = new LNode;
L->next = NULL;
cout << "OK" << endl;
}
L = new LNode;
L->next = NULL;
cout << "OK" << endl;
}
// 判断链表是否为空
void PanDuan(LinkList L) {
if (L->next == NULL) {
cout << "单链表为空。" << endl;
}
else {
cout << "单链表不为空。" << endl;
}
}
if (L->next == NULL) {
cout << "单链表为空。" << endl;
}
else {
cout << "单链表不为空。" << endl;
}
}
// 删除单链表
void ShanChu(LinkList& L) {
LNode* P;
while (L) {
P = L;
L = L->next;
delete P;
}
cout << "单链表已经被删除了。" << endl;
}
LNode* P;
while (L) {
P = L;
L = L->next;
delete P;
}
cout << "单链表已经被删除了。" << endl;
}
// 清空单链表
void Clear1(LinkList& L) {
LNode *P, *Q;
P = L->next;
while (P) {
Q = P;
P = P->next;
delete Q;
}
L->next = NULL;
cout << "单链表已经被清空了。" << endl;
}
void Clear2(LinkList& L) {
LNode* P, *Q;
P = L->next;
while (P) {
Q = P->next;
delete P;
P = Q;
}
L->next = NULL;
cout << "单链表已经被清空了。" << endl;
}
LNode *P, *Q;
P = L->next;
while (P) {
Q = P;
P = P->next;
delete Q;
}
L->next = NULL;
cout << "单链表已经被清空了。" << endl;
}
void Clear2(LinkList& L) {
LNode* P, *Q;
P = L->next;
while (P) {
Q = P->next;
delete P;
P = Q;
}
L->next = NULL;
cout << "单链表已经被清空了。" << endl;
}
// 链表的表长
int BiaoChang1(LinkList L) {
int count = 0;
LNode* p, *q;
p = L->next;
while (p) {
count++;
q = p->next;
p = q;
}
return count;
}
int BiaoChang2(LinkList L) {
int count = 0;
LNode* p;
p = L->next;
while (p) {
count++;
p = p->next;
}
return count;
}
int count = 0;
LNode* p, *q;
p = L->next;
while (p) {
count++;
q = p->next;
p = q;
}
return count;
}
int BiaoChang2(LinkList L) {
int count = 0;
LNode* p;
p = L->next;
while (p) {
count++;
p = p->next;
}
return count;
}
// 取值
void QuZhi1(LinkList L, int i) {
LNode* P;
if (i<1 || i>BiaoChang1(L)) {
cout << "位置不合法。" << endl;
return;
}
P = L->next;
for (int j = 1; j < i; j++) {
P = P->next;
}
cout << P->date << endl;
}
void QuZhi2(LinkList L, int i) {
LNode* P;
P = L->next;
int j = 1;
while (P && j < i) {
P = P->next; j++;
}
if (!P || j > i) {
cout << "位置不合法。" << endl;
}
else
cout << P->date << endl;
}
LNode* P;
if (i<1 || i>BiaoChang1(L)) {
cout << "位置不合法。" << endl;
return;
}
P = L->next;
for (int j = 1; j < i; j++) {
P = P->next;
}
cout << P->date << endl;
}
void QuZhi2(LinkList L, int i) {
LNode* P;
P = L->next;
int j = 1;
while (P && j < i) {
P = P->next; j++;
}
if (!P || j > i) {
cout << "位置不合法。" << endl;
}
else
cout << P->date << endl;
}
// 查找值
void ChaZhao(LinkList L, int i) {
LNode* P;
P = L->next;
int j = 1;
while (P && P->date != i) {
P = P->next;
j++;
}
if (P) {
cout << "与值" << i << "相同的值的地址是:" << P << endl;
cout << "与值" << i << "相同的值的在线性表的位置是:" << j << endl;
}
else {
cout << "没有与值相同的值。";
}
}
LNode* P;
P = L->next;
int j = 1;
while (P && P->date != i) {
P = P->next;
j++;
}
if (P) {
cout << "与值" << i << "相同的值的地址是:" << P << endl;
cout << "与值" << i << "相同的值的在线性表的位置是:" << j << endl;
}
else {
cout << "没有与值相同的值。";
}
}
// 插入操作
void ChaRu1(LinkList& L, int i, int e) {
LNode* P, *S;
int k = BiaoChang1( L);
if (i<1 && i>k + 1) {
cout << "插入的位置不合法." << endl;
return;
}
P = L;
for (int j = 0; j < i - 1; j++) {
P = P->next;
}
S = new LNode;
S->date = e;
S->next = P->next;
P->next = S;
}
void ChaRu2(LinkList& L, int i, int e) {
LNode* P, *S;
P = L;
int j = 0;
while (P->next && j < i - 1) {
P = P->next; j++;
}
if (!(P->next) || j > i - 1) {
cout << "插入的位置不合法." << endl;
return;
}
S = new LNode;
S->date = e;
S->next = P->next;
P->next = S;
}
LNode* P, *S;
int k = BiaoChang1( L);
if (i<1 && i>k + 1) {
cout << "插入的位置不合法." << endl;
return;
}
P = L;
for (int j = 0; j < i - 1; j++) {
P = P->next;
}
S = new LNode;
S->date = e;
S->next = P->next;
P->next = S;
}
void ChaRu2(LinkList& L, int i, int e) {
LNode* P, *S;
P = L;
int j = 0;
while (P->next && j < i - 1) {
P = P->next; j++;
}
if (!(P->next) || j > i - 1) {
cout << "插入的位置不合法." << endl;
return;
}
S = new LNode;
S->date = e;
S->next = P->next;
P->next = S;
}
// 删除操作
void ShanChu(LinkList& L, int i) {
LNode* P, *S;
P = L;
int j = 0;
while (P->next && j < i - 1) {
P = P->next; j++; // P->next=P->next->next;
}
if (!(P->next) || j > i - 1)
{
cout << "删除的位置不合法." << endl;
return;
}
S = P->next;
P->next = S->next;
delete S;
}
LNode* P, *S;
P = L;
int j = 0;
while (P->next && j < i - 1) {
P = P->next; j++; // P->next=P->next->next;
}
if (!(P->next) || j > i - 1)
{
cout << "删除的位置不合法." << endl;
return;
}
S = P->next;
P->next = S->next;
delete S;
}
// 初始化链表:头插法
void TouChaFa(LinkList& L, int n) {
LNode* P;
for (int i = n; i > 0; i--) {
P = new LNode;
cin >> P->date;
P->next = L->next;
L->next = P;
}
}
LNode* P;
for (int i = n; i > 0; i--) {
P = new LNode;
cin >> P->date;
P->next = L->next;
L->next = P;
}
}
// 初始化链表:尾插法
void WeiChaFa(LinkList& L, int n) {
LNode* P, *Q;
Q = L;
for (int i = n; i > 0; i--) {
P = new LNode;
P->next = NULL;
cin >> P->date;
Q->next = P;
Q = P;
}
}
LNode* P, *Q;
Q = L;
for (int i = n; i > 0; i--) {
P = new LNode;
P->next = NULL;
cin >> P->date;
Q->next = P;
Q = P;
}
}
int main() {
LinkList L;
JianZao(L);
PanDuan( L);
int n;
cin >> n;
WeiChaFa( L, n);
PanDuan( L);
cout << BiaoChang2(L) << endl;
QuZhi1(L, 4);
QuZhi2(L, 4);
ChaZhao(L, 6);
ChaRu2(L, 4, 9);
QuZhi2(L, 4);
ShanChu(L, 4);
QuZhi2(L, 4);
}
LinkList L;
JianZao(L);
PanDuan( L);
int n;
cin >> n;
WeiChaFa( L, n);
PanDuan( L);
cout << BiaoChang2(L) << endl;
QuZhi1(L, 4);
QuZhi2(L, 4);
ChaZhao(L, 6);
ChaRu2(L, 4, 9);
QuZhi2(L, 4);
ShanChu(L, 4);
QuZhi2(L, 4);
}
浙公网安备 33010602011771号