#include<iostream>
using namespace std;
int n,i,choice,elem;
typedef struct LNode{
int element;
struct LNode *next;
}LNode,*LinkList;
void init(LinkList &L){
L = new LNode;
L -> next = NULL;
}
void LinkOutput(LinkList &L){
LinkList p = new LNode;
p = L -> next;
while (p){
cout << p -> element << ' ';
p = p -> next;
}
cout << endl;
}
void LinkInsert(LinkList &L,int i,int elem){
LinkList p = new LNode;
int j = 0;
p = L;
while (p && j < i - 1){
p = p -> next;
j ++;
}
if (p){
LinkList q = new LNode;
q -> element = elem;
q -> next = p -> next;
p -> next = q;
}
LinkOutput(L);
cout << "Insert data successfully" << endl;
}
void Linkdelete(LinkList &L,int i){
LinkList p = new LNode;
LinkList q = new LNode;
p = L;
int j = 0;
while (p && j < i - 1){
p = p -> next;
j ++;
}
if (p -> next){
q = p -> next;
p -> next = q -> next;
delete q;
}
LinkOutput(L);
cout << "LNode deleted successfully" << endl;
}
int main(){
LinkList L;
init(L);
cout << "Enter n:" << endl;
cin >> n;
for (int k = 1;k <= n;k ++){
cout << "Enter choice:" << endl;
cin >> choice;
if (choice == 1){
cout << "Enter i and elem" << endl;
cin >> i >> elem;
LinkInsert(L,i,elem);
}else if(choice == 2){
cout << "Enter i" << endl;
cin >> elem;
Linkdelete(L, elem);
}
}
}
[求助帖]VS Code新手用户 为什么以上单链表无法在VS Code中运行,但可在VS 2022完美运行
最新推荐文章于 2024-08-18 18:02:49 发布