C++ 在C+中打印列表中的元素以及另一个列表中的位置+;
您将得到一个列表L和另一个列表p,其中包含按升序排序的整数。操作C++ 在C+中打印列表中的元素以及另一个列表中的位置+;,c++,list,pointers,stl,C++,List,Pointers,Stl,您将得到一个列表L和另一个列表p,其中包含按升序排序的整数。操作printLots(L,P)将打印位于P指定位置的L中的元素。例如,如果P=1、3、4、6,则打印位于L中位置1、3、4和6的元素。编写程序printLots(L,P)。您只能使用公共STL容器操作。您的过程的运行时间是多少 这就是我目前所拥有的。一切都会编译,但是在运行它并使用选项3之后,什么都不会打印,我也不确定我做错了什么 起初,我尝试使用多种选项来添加元素等,但如果有人能帮我解决问题的基本方面,或者帮我解决问题,我将不胜感激
printLots(L,P)
将打印位于P指定位置的L中的元素。例如,如果P=1、3、4、6
,则打印位于L中位置1、3、4和6的元素。编写程序printLots(L,P)
。您只能使用公共STL
容器操作。您的过程的运行时间是多少
这就是我目前所拥有的。一切都会编译,但是在运行它并使用选项3之后,什么都不会打印,我也不确定我做错了什么
起初,我尝试使用多种选项来添加元素等,但如果有人能帮我解决问题的基本方面,或者帮我解决问题,我将不胜感激
#include <iostream>
#include <stdlib.h>
#include <cstdio>
#define NULL 0
using namespace std;
struct node
{
int elements;
struct node *link;
};
typedef struct node NODE;
NODE* create(NODE* h, int P);
int count(NODE* h);
NODE* sort(NODE* h);
void printLots(NODE* L, NODE* P);
NODE* create(NODE* h, int P)
{
if (h == NULL)
{
h = (NODE*)malloc(sizeof(NODE));
h->elements = P;
h->link = NULL;
}
else
{
h->link = create(h->link, P);
}
return (h);
printf("h");
}
int count(NODE* h)
{
int count = 0;
if (h != NULL)
{
count++;
h = h->link;
}
return count;
}
NODE* sort(NODE* h)
{
int i, q, r, temp;
NODE* htemp = h;
i = count(h);
for (q = 0; q < i; q++)
{
for (r = 0; r < i - 1; r++)
{
if (h->elements > h->link->elements)
{
temp = h->elements;
h->elements = h->link->elements;
h->link->elements = temp;
}
h = h->link;
}
h = htemp;
}
return htemp;
}
void printLots(NODE* L, NODE* P)
{
int countL, countP, i, j;
NODE* temp = L;
P = sort(P);
L = sort(L);
countL = count(L);
countP = count(P);
for (i = 0; i < countP; i++)
{
if (P->elements > countL)
{
printf("The L list does not contain %d elements\n", P->elements);
}
else
{
for (j = 0; j < P->elements - 1; j++)
{
L = L->link;
printf("%d\n", L->elements);
L = temp;
}
}
P = P->link;
}
}
int main()
{
int cs, value;
NODE *L, *P;
L = NULL;
P = NULL;
while (1)
{
printf("1.Add to list L\n");
printf("2.Add to list P\n");
printf("3.Print List\n");
printf("4.Clear Screen\n");
printf("5.Exit\n");
printf("Enter your choice: ");
scanf_s("%d", &cs);
switch (cs)
{
case 1: scanf_s("%d", &value);
L = create(L, value);
break;
case 2: scanf_s("%d", &value);
P = create(P, value);
break;
case 3: printLots(L, P);
break;
case 4: system("cls");
break;
case 5: exit(1);
break;
}
}
}
#包括
#包括
#包括
#定义空0
使用名称空间std;
结构节点
{
int元素;
结构节点*链接;
};
typedef结构节点;
节点*创建(节点*h,int P);
整数计数(节点*h);
节点*排序(节点*h);
无效打印批次(节点*L,节点*P);
节点*创建(节点*h,int P)
{
if(h==NULL)
{
h=(节点*)malloc(节点大小);
h->元素=P;
h->link=NULL;
}
其他的
{
h->link=create(h->link,P);
}
返回(h);
printf(“h”);
}
整数计数(节点*h)
{
整数计数=0;
如果(h!=NULL)
{
计数++;
h=h->link;
}
返回计数;
}
节点*排序(节点*h)
{
内部i、q、r、温度;
节点*htemp=h;
i=计数(h);
对于(q=0;qelements>h->link->elements)
{
temp=h->elements;
h->elements=h->link->elements;
h->链接->元素=温度;
}
h=h->link;
}
h=htemp;
}
返回htemp;
}
无效打印批次(节点*L,节点*P)
{
int countL,countP,i,j;
节点*temp=L;
P=排序(P);
L=排序(L);
countL=计数(L);
countP=计数(P);
对于(i=0;ielements>countL)
{
printf(“L列表不包含%d个元素\n”,P->elements);
}
其他的
{
对于(j=0;jelements-1;j++)
{
L=L->链接;
printf(“%d\n”,L->elements);
L=温度;
}
}
P=P->link;
}
}
int main()
{
int-cs,值;
节点*L,*P;
L=零;
P=零;
而(1)
{
printf(“1.Add to list L\n”);
printf(“2.Add to list P\n”);
printf(“3.Print List\n”);
printf(“4.Clear Screen\n”);
printf(“5.退出\n”);
printf(“输入您的选择:”);
扫描单位(“%d”和“cs”);
开关(cs)
{
案例1:scanf_s(“%d”&值);
L=创建(L,值);
打破
案例2:scanf_s(“%d”&值);
P=创建(P,值);
打破
案例3:打印件(L,P);
打破
案例4:系统(“cls”);
打破
案例5:出口(1);
打破
}
}
}
当您使用编译器的调试器逐步检查代码时,调试器显示了您的代码做了什么?如果您还没有使用调试器,为什么不呢?另外,既然你的指导者允许你使用STL容器,为什么你在这里重新发明了这么多的轮子?在Visual Studio 2013中使用我的本地Windows调试器,输出中显示的所有内容都是“找不到或打开PDB文件”。但是,就是这样。为什么当它不使用时,这个标记的C++和STL?代码是C。那么您没有正确使用调试器。一个简单的谷歌搜索结果表明这些信息是无害的。您知道如何使用调试器设置断点,并逐步完成代码,对吗?当你在PrimtLoSt()函数中设置断点,然后逐步通过后续代码时,你看到代码在做什么?好吧,没有人曾经说过C++是简单的。在处理非平凡的C++代码量时,如我所说,知道如何使用调试器是每个C++开发人员的一项强制技能。如果你的指导者还没有显示和解释如何调试C++代码,那是不幸的,因为在这之前,这应该是一个必要的学习材料。