I implemented generic stack in C. Seems to work fine. It doesn't work for strings, though.
It was compiled using Visual C++.
Implementation:
#include <stdlib.h>
#include <stdio.h>
#include <iostream>
#include <string>
struct node{
void* value;
node* next;
};
struct stack{
node* first;
int elemSize;
int length;
};
stack* StackNew(int elemSize)
{
stack* s = (stack*)malloc(sizeof(stack));
s->elemSize = elemSize;
s->length = 0;
return s;
}
void Push(stack* s, void* value)
{
node* oldFirst = s->first;
s->first = (node*)malloc(sizeof(node));
s->first->value = malloc(s->elemSize);
memcpy(s->first->value, value, s->elemSize);
s->first->next = oldFirst;
s->length++;
}
void* Pop(stack* s)
{
void* value = malloc(s->elemSize);
memcpy(value, s->first->value, s->elemSize);
node* oldFirst = s->first;
s->first = s->first->next;
free(oldFirst);
s->length--;
return value;
}
void Destory(stack* s)
{
while (s->length > 0) Pop(s);
free(s);
}
Test:
struct point{
int x, y;
};
int main()
{
stack* s = StackNew(sizeof(point));
for (int i = 0; i < 100; i++){
point pt;
pt.x = i;
pt.y = i * i;
Push(s, &pt);
}
while (s->length > 0) {
point pt = *(point*)Pop(s);
printf("(%d,%d)\n", pt.x, pt.y);
}
Destory(s);
return 0;
}
Output:
(99,9801) (98,9604) (97,9409) (96,9216) (95,9025) (94,8836) (93,8649) (92,8464) (91,8281) (90,8100) (89,7921) (88,7744) (87,7569) (86,7396) ...
as expected.
Please tell me what I did wrong and how I can improve things.
iostream
? – EngieOP 13 hours ago#include <iostream>
and retag as c. – 200_success♦ 12 hours ago