0

You are given a list, L, and another list, P, containing integers sorted in ascending order. The operation printLots(L,P) will print the elements in L that are in positions specified by P. For instance, if P = 1, 3, 4, 6, the elements in positions 1, 3, 4, and 6 in L are printed. Write the procedure printLots(L,P). You may use only the public STL container operations. What is the running time of your procedure?

This is what I have so far. Everything compiles, however after running it and using option 3, nothing is printed and I'm not sure what I've done wrong.

At first I was trying to make it with multiple options to where you could add elements, etc., but if someone could please just help me with the basic aspects of the question or help me fix what I have, that would be greatly appreciated.

#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;
        }
    }
}
Embedded C
  • 1,448
  • 3
  • 16
  • 29
lodroid
  • 1
  • 2
  • And what did your debugger show your code did, when you stepped through your code using your compiler's debugger? And, if you haven't used the debugger yet, why not? Additionally, since your instructor allows you to use STL containers, why did you reinvent so many wheels here? – Sam Varshavchik Mar 14 '16 at 01:48
  • Using my local windows debugger in Visual Studio 2013, all it shows in the output is a lot of "Cannot find or open the pdb file." but that's it. – lodroid Mar 14 '16 at 02:00
  • Why is this tagged C++ and STL when it doesn't use either? The code is straight C. – Pete Becker Mar 14 '16 at 02:03
  • Then you're not using your debugger correctly. A simple Google Search finds http://stackoverflow.com/questions/15937707/error-message-cannot-find-or-open-the-pdb-file which indicates that those messages are harmless. You do know how to use your debugger to set a breakpoint, and step through the code, right? When you set a breakpoint at your printLots() function, and then stepped through the subsequent code, what did you see the code doing? – Sam Varshavchik Mar 14 '16 at 02:06
  • Sam I appreciate it. I'm still in college and I'm trying to teach myself how to do this. Pretty much everything I have done is from me trying to go through libraries and figure this stuff out. – lodroid Mar 14 '16 at 02:07
  • Pete I'm sorry I am just trying to figure out how to do my homework assignment and I obviously may have done this completely wrong.... – lodroid Mar 14 '16 at 02:08
  • 1
    Well, nobody has ever claimed that C++ is easy. When dealing with non-trivial amount of C++ code, knowing how to use a debugger is, as I said, a mandatory skill for every C++ developer. If your instructor hasn't already showed and explained to you how to debug C++ code, that's unfortunate, because this should've been a necessary material to learn, before this point. – Sam Varshavchik Mar 14 '16 at 02:08
  • tip: hard code your elements, then try to print them. Once it works, collect your elements from the user's input – Khalil Khalaf Mar 14 '16 at 02:08
  • If I'm going completely in the wrong direction, could one of you please give me some guidance on how I should have done this problem? – lodroid Mar 14 '16 at 02:14
  • remove all printf/scanf and their loops, and start by assigning test values within the code. Step over your code "press F10 or go to Debug->Step Over". Once you make sure the behavior is correct, then take the input from the user and proceed with the same behavior – Khalil Khalaf Mar 14 '16 at 02:38

0 Answers0