表、栈、队列联系

来源:互联网 发布:利欧数字网络 编辑:程序博客网 时间:2024/06/05 23:59

3.2给你一个链表L和另一个链表P,它们包含以升序排序的整数。操作PrintLots(L,P),将打印L中那些由P所指定的位置上的元素。

`#include“iostream”
using namespace std;

typedef struct Node {
int number;
Node* next;
}Node;

//初始化单链表
Node* initNode() {
Node* first = new Node();
if (first == NULL) {
cout << “链表初始化失败”;
exit(-1);
}
first->next = NULL;
return first;
}
//头插法赋值
void headAddNode(int number, Node* first) {
Node* temp = new Node();
if (temp == NULL) {
cout << “链表初始化失败”;
exit(-1);
}
else {
temp->number = number;
temp->next = first->next;
first->next = temp;
}
}
//取出指定位置的元素值
void getValue(int sit, Node* first) {
Node* temp = first;
int i = 0;
while (temp->next != NULL) {
temp = temp->next;
i++;
if (i == sit) {
cout<number;
break;
}
}
if (i != sit) {
cout << “不存在这样位置的元素”;
}
}
void PrintLots(Node* L, Node* P) {
Node* temp1 = L;
Node* temp2 = P;
while (temp2->next != NULL) {
temp2 = temp2->next;
int temp = temp2->number;
getValue(temp, temp1);
}
}

int main() {
Node* l = initNode();
for (int i = 0; i < 10; i++) {
headAddNode(i, l);
}
Node* p = initNode();
headAddNode(1,p);
headAddNode(3, p);
headAddNode(4, p);
headAddNode(6, p);
PrintLots(l, p);
int k;
cin >> k;
return 0;
}`

原创粉丝点击