1

I have a stack of numbers from 0-17 and i need to put that stack into List on first position how could i use function Insert? or do i have to somehow change Insert ?

This is my struct of List

struct List
{
    int data;
    struct List *Next;
};

and this is how i have my Insert

void Insert(List **pps, int prvek)
{
    List *ps;
    ps = (List *)malloc(sizeof(List));
    if (!ps) {
        return;
    }
    ps->data = prvek;
    ps->Next = *pps;
    *pps = ps;
}
Kasik
  • 41
  • 4
  • you forgot to add a tag for the programming lang – Adam Siemion Mar 27 '15 at 09:22
  • define what you mean with "I have a stack of..."; I think it's unclear. Also, what *exactly* do you want to insert into what? What kind of items does your list contain? – Marcus Müller Mar 27 '15 at 09:24
  • A list where you only insert and remove at the front is a stack. – Some programmer dude Mar 27 '15 at 09:25
  • Your insert function looks fine to me , what problem are you facing ? – Kunal Mar 27 '15 at 09:26
  • I think that this List can just be used a stack as it is putting a new thing at the top; so this `Insert` in list is doing a `Push` on stack. – Ashish Negi Mar 27 '15 at 09:27
  • Standard Warning : Please [do not cast](http://stackoverflow.com/q/605845/2173917) the return value of `malloc()` and family in `C`. – Sourav Ghosh Mar 27 '15 at 09:27
  • Where did you copy this code? AND: please get the formatting right! – DrKoch Mar 27 '15 at 09:28
  • It's not very clear what you want to achieve. You can't have all 18 numbers "on the first position" at the head of the list. The `List` only holds integers, so you cannot put a whole 'stack' on it at all. A stack is a [LIFO](http://en.wikipedia.org/wiki/LIFO_(computing)) storage system, so what order would the have to be added to the list: eldest at the head or newest? Please could you provide some code to show what you've tried, as well as the implementation of your 'stack'? – Evil Dog Pie Mar 27 '15 at 09:30
  • @Kunal I need to insert into List this first Stack and then i will from that first stack take for example 4 numbers (14,15,16,17)and put it into another stuck and that second stack will be on 2nd position in that List. – Kasik Mar 27 '15 at 09:42
  • @Kasik, so you want to make list/array of stack ? like [first stack, second stack, ..., ... ] ? – Kunal Mar 27 '15 at 09:47
  • @MikeofSST i need a pointer in that list on that stack of numbers and in a code for now i only generate that stack full of numbers and i wrote comments here what exactly i need :) – Kasik Mar 27 '15 at 09:55
  • @Kasik, then you need a Vector, which holds the stack pointer(which is your current List struct). Once you create a Stack insert the head pointer to Vector. – Kunal Mar 27 '15 at 09:56
  • As you're using C++, have you looked at using `std::List` ? You could declare `std:List` variable and use that. – Evil Dog Pie Mar 27 '15 at 10:05
  • @MikeofSST well i have forbidden to use classes -_- so how exactly can i use it? – Kasik Mar 27 '15 at 10:57

1 Answers1

0

So from your comment, I understood that you need a List of stacks. Your current code already is able to create stack. You can store that stack to vector.

Like below,

#include <vector>

std::vector<List*> stackList;

stackList.push_back(firstStack);
stackList.push_back(secondStack);

....

Kunal
  • 3,475
  • 21
  • 14
  • @Kunal It gives me this error :/ at `stackList.push_back(T);` IntelliSense: no instance of overloaded function "std::vector<_Ty, _Alloc>::push_back [with _Ty=List *, _Alloc=std::allocator]" matches the argument list argument types are: (Stack) object type is: std::vector> – Kasik Mar 27 '15 at 10:43