纯代码,想看懂只需要了解一下指针即可。
#include <iostream>
#include <stdio.h>
using namespace std;
typedef struct LNode {
int data;
struct LNode* next;
};
void Judge(bool judge) {
if (judge == true)
cout << "Opration Successful\n";
else
cout << "Opration failed\n";
}
bool AddNode(int num, LNode *H) {
try {
int x; LNode *S = H;
LNode* R = (LNode*)malloc(sizeof(LNode));
for (int i = 1; i <= num; i++) {
if (S == NULL) throw 1;
S = S->next;
}
R->next = S->next;
S->next = R;
cout << "Please input the value\n";
cin >> x; R->data = x;
return true;
}
catch (int e) {
return false;
}
}
bool SearchValueByOrderNumber(int num, LNode *H) {
try {
LNode *S = H;
for (int i = 1; i <= num; i++) {
S = S->next;
if (S == NULL) throw 1;
}
cout << "The value you are searching for is "<< S->data << "\n";
return true;
}
catch (int e) {
return false;
}
}
bool SearchOrderNumberByValue(int num, LNode *H) {
try {
LNode* S = H;
cout << "The order number you are looking for is ";
for (int i = 1; i <= INT_MAX; i++) {
S = S->next;
if (S->data == num && S != NULL) {
cout << i << "\n";
break;
}
else if (S->next == NULL) throw 1;
}
return true;
}
catch (int e) {
return false;
}
}
bool DeleteNodeByOrderNumber(int num, LNode *H) {
try {
LNode* S = H;
LNode* R;
for (int i = 1; i < num; i++) {
if (S->next == NULL) throw 1;
S = S->next;
}
R = S->next;
S->next = R->next;
free(R);
return true;
}
catch (int e) {
return false;
}
}
bool DeleteNodeByValue(int num, LNode *H) {
try {
LNode* S = H;
LNode* R;
for (int i = 1; i <= INT_MAX; i++) {
if (S->next == NULL) throw 1;
if (S->next->data == num) {
break;
}
S = S->next;
}
R = S->next;
S->next = R->next;
free(R);
return true;
}
catch (int e) {
return false;
}
}
bool InitializeLinkList(LNode* &H) {
try {
LNode* S; int x;
H = (LNode*)malloc(sizeof(LNode));
H->next = NULL;
for (int i = 1; i <= INT_MAX; i++) {
S = (LNode*)malloc(sizeof(LNode));
cin >> x; if (x == -1) break;
S->data = x;
S->next = H->next;
H->next = S;
}
return true;
}
catch (exception e) {
return false;
}
}
bool ReadLinkList(LNode* H) {
try {
LNode* S = H->next;
for (int i = 1; i <= INT_MAX; i++) {
if (S == NULL) break;
cout << S->data << " ";
S = S->next;
}
cout << "\n";
return true;
}
catch(exception e){
return false;
}
}
int main() {
LNode *Head;
cout << "Initialize the linklist, input -1 to stop\n";
InitializeLinkList(Head);
cout << "Input 1 to ReadLinkList\n"
"Input 2 to DeleteNodeByOrderNumber\n"
"Input 3 to DeleteNodeByValue\n"
"Input 4 to AddNode\n"
"Input 5 to SearchValueByOrderNumber\n"
"Input 6 to SearchOrderNumberByValue\n"
"Input anyothers to end\n";
int choice,num;
bool judge;
while (1) {
cin >> choice;
switch (choice){
case 1:
judge = ReadLinkList(Head);
Judge(judge);
break;
case 2:
cout << "Please input the order number\n";
cin >> num;
judge = DeleteNodeByOrderNumber(num, Head);
Judge(judge);
break;
case 3:
cout << "Please input the node value\n";
cin >> num;
judge = DeleteNodeByValue(num, Head);
Judge(judge);
break;
case 4:
cout << "Please input the order number\n";
cin >> num;
judge = AddNode(num, Head);
Judge(judge);
break;
case 5:
cout << "Please input the order number of the node you are searching for\n";
cin >> num;
judge = SearchValueByOrderNumber(num, Head);
Judge(judge);
break;
case 6:
cout << "Please input the value of the node you are searching for\n";
cin >> num;
judge = SearchOrderNumberByValue(num, Head);
Judge(judge);
break;
default:
return 0;
}
}
return 0;
}
|