struct stack
{
int value;
struct stos *w;
};
struct stack *pnt;
struct stack *prev;
void push(value);
void delete(struct stack *new);
void print_stack();
void push(int x)
{
prev = pnt;
pnt = (struct stack*)malloc(sizeof(struct stack));
pnt->value=x;
pnt->w = prev;
printf("Top of stack: %d\n", pnt->value);
}
void delete(struct stack *new)
{
if (new!=NULL)
{
prev = new->w;
printf("Deleted: %d\n", new->value);
free(new);
pnt = prev;
}
else printf("Stack is empty\n");
}
void print_stack()
{
printf("Content of stack:\n");
prev = pnt;
while (prev!=NULL)
{
printf("%d\n", prev->value);
prev = prev->w;
}
}
I have a question about the pointers there. Not what every pointer means (probably means top of stack) but what is meant by 'prev' and 'pnt'.
I just don't get it at all. 'Prev' maybe is a previous value of stack?
Maybe somebody can show me a picture.
pnt is a pointer, as declared by,
struct stack *pnt;
struct stack *prev;
it points to a stack struck declared by
struct stack
{
int value;
struct stos *w; // struct stos should struct stack
};
You are missing this declaration from the other code.
typedef struct stos{
int value;
struct stos *next;
} stos;
So, this is lifted code that has problems, a lot of problems...
Let us begin
First fix the stos mess:
The problems the two structs above were lifted (copied/pasted) so instead of the struct stack containing a pointer to same type of structure is looking for an undefined structure struct stos *next; it should be struct stack* next;
typedef struct Stack;
typedef struct stack {
int value;
Stack* next;
} Stack;
struct stack *pnt; // pnt = pointer top of stack
struct stack *prev; // prev = pointer to the previous stack item
So now we know the stack is kept as a singularly linked list of stack structs.
Let's decipher void push(int x)
void push(int x)
{
// point prev to pnt, previous to top of stack
prev = pnt;
// create a new item to place on the top of the stack
pnt = (struct stack*)malloc(sizeof(struct stack));
// initial the value member of the new item with x
pnt->value=x;
// point the new item's link point to point the previous top of the stack
pnt->w = prev;
// print the value contained in the new top of the stack item.
printf("Top of stack: %d\n", pnt->value);
}
This function is a disaster because it use the keyword new as the name of a variable. Yes, new is in C++. But why make a mess of things for the next person.
void delete(struct stos *new)
{
if (new!=NULL)
{
prev = new->w;
printf("Deleted: %d\n", new->value);
free(new);
pnt = prev;
}
else printf("Stack is empty\n");
}
Should have been written as follows...
// forward declaration of Stos because it won't be defined until the end of struct it is used in.
typedef struct {
int value;
struct Stack* next;
} Stack;
Stack* pnt; // pnt = pointer top of stack
Stack* prev; // prev = pointer to the previous stack item
Using Stack* pnt versus Stack *pnt says, "A Stack pointer called pnt" versus "A pointer called pnt which points to Stacks". (There is also a spiral trick to read these declarations easier Q.E.D)
void delete( Stack* pNew)
{
if ( pNew )
{
prev = pNew->w;
printf("Deleted: %d\n", pNew->value);
free(pNew);
pnt = prev;
}
else printf("Stack is empty\n");
}
As I said, this is a mess.
What is important is you asked a question and hopefully have learned a few things that will help you on your next piece of code.
once you create a typedef, then use it. drop the struct stack stuff
make the names of variables long enough so that future readers will understand the code Stack* pTopOfStack;
try not to use words that are keywords in derivatives new
Don't over crowd the code with redundant code
if ( new != NULL ) versus if ( new ), even better if ( pNewStackItem )
Better to fully understand what the code is doing instead of complicating it with more stuff that looks right, compiles and runs. Never trust a programmer, "sorry, it works on my machine."
Related
The main code itself works fine but I want to make use of character strings instead of the char, and it might be because I've overlooked something absurdly simple. Here is a small snippet of code:
struct Stack
{
char *SData;
int counter;
struct Stack *next;
};
struct Stack* StackNewNode(char SData)
{
struct Stack *stackNode = (struct Stack*)malloc(sizeof(struct Stack));
stackNode->SData = SData; //error here because of the difference of char, should i use []?
stackNode->next = NULL;
return stackNode;
}
void PUSH(struct Stack **root, char SData) //this part only pushes a character
{
struct Stack *stackNode = StackNewNode(SData);
stackNode->next = *root;
*root = stackNode;
printf("\n%c pushed to stack\n", SData);
}
void POP(struct Stack **root)
{
if (*root == NULL)
{
return;
}
struct Stack *temp = *root;
*root = (*root)->next;
char pop = temp->SData; //how should i pop a full string?
free(temp);
printf("%c popped from stack\n", pop);
}
I also want to implement a counter in the Stack. Should i use Stack->counter++ or is there some other more correct way to do it?
Sorry for the wall of text its my first time here
I don't see counter in struct Stack used anywhere in the code snippets you provided. Assuming you want some count of the number of elements in the stack, it wouldn't make sense for this value to be part of the struct. After all, you only need one count, not one attached to every element in the stack. You would want to make this its own variable, perhaps a global defined in the same place as you define root. Just make sure to initialize it to zero.
As for using character strings, you already have char *SData, which can point to a string. All you need to do is change PUSH and StackNewNode to use char* parameter rather than char and then pass it a string, something like this:
PUSH(&root, "My string");
To start with your current code have some type mismatch here: stackNode->SData = SData;
stackNode->SData is a char pointer and SData is a char. I assume your compiler warns you about that - never ignore warnings.
If you want to use C type string you need updates like:
Pass char pointer instead of char
Allocate memory for the string
Copy the passed string to the allocated memory
Free the memory when done
Something like:
struct Stack
{
char *SData;
int counter;
struct Stack *next;
};
struct Stack* StackNewNode(const char* str)
{
struct Stack *stackNode = malloc(sizeof(struct Stack));
if (stackNode == NULL) exit(1);
stackNode->SData = malloc(strlen(str) + 1);
if (stackNode->SData == NULL) exit(1);
strcpy(stackNode->SData, str);
stackNode->next = NULL;
return stackNode;
}
void PUSH(struct Stack **root, const char* str)
{
struct Stack *stackNode = StackNewNode(str);
stackNode->next = *root;
*root = stackNode;
printf("\n%s pushed to stack\n", str);
}
void POP(struct Stack **root)
{
if (*root == NULL)
{
return;
}
struct Stack *temp = *root;
*root = (*root)->next;
printf("%s popped from stack\n", temp->SData);
free(temp->SData);
free(temp);
}
Usage example:
struct Stack *root = NULL;
PUSH(&root, "Hello World");
Adding a counter
In your code you have placed int counter; inside every stack element. You can make that work but I would prefer two structs. One struct type holding information about the whole stack and another struct type for the elements.
Like:
struct StackNode
{
char *SData;
struct StackNode *next;
};
struct Stack
{
int counter;
struct StackNode *root;
};
The functions would need some updates like:
void PUSH(struct Stack *stack, const char* str)
{
struct StackNode *stackNode = StackNewNode(str);
stackNode->next = stack->root;
stack->root = stackNode;
++stack->counter; // Increment counter
printf("\n%s pushed to stack\n", str);
}
Usage example:
struct Stack stack = {0, NULL};
PUSH(&stack, "Hello World");
I made my own stack using linked list. But I think this is wrong.
my push method is linking Stack1 to other stacks.
So, I think it is like...
In my main function,
push(stack1, 10);
push(stack1, 20);
[Stack1] -> [nextStack]
[Stack1] -> [nextStack] (new address from first nextStack)
So, It's like... I am repeating to link stack1 to other stacks again and again...
this is my stack using linked list code below.
#include <stdio.h>
#include <stdlib.h>
typedef struct{
int data;
struct stack *top;
}stack;
void push(stack *currentStack, int data){
if (currentStack->top == NULL)
fprintf(stderr, "Stack is emtpy");
else{
stack *nextStack = (stack*)malloc(sizeof(stack));
currentStack->data = data;
currentStack->top = nextStack;
printf("currentStack is %d\n", currentStack->data);
}
}
int main(){
stack* stack1;
stack1 = (stack*)malloc(sizeof(stack));
push(stack1, 10);
push(stack1, 20);
return 1;
}
and this is the result of my code.
currentStack is 10
currentStack is 20
#include <stdio.h>
#include <stdlib.h>
struct stack
{
int data;
struct stack *top;
} *head = NULL;
void push(int data)
{
if (head == NULL) //that means stack is empty
{
head =(struct node *)malloc(1*sizeof(struct node));
head->top = NULL;
head->data = data;
}
else
{
temp =(struct node *)malloc(1*sizeof(struct node));
temp->top = head;
temp->data = data;
head = temp;
}
}
Your push() function is incomplete.
It should consider two cases one when stack is empty and one when it is not.
Also there is no need to pass a pointer-to-stack in push() function because push() function by default pushes the new element on the topmost node and there is only one stack.
Also you have not initialised your stack pointer with NULL. This might give you undefined behaviour during program run.
I'm very new to C and I'm trying yet to fully understand it. I implemented a stack
but found trouble when making its destructor and it constructor/init.
Are these well done?
These are the typedefs for the structures used by the stack:
typedef struct Node{
void* cargo;
struct Node* next;
}Node;
typedef struct Stack{
int size;
Node* firstOut;
}Stack;
And these are the functions:
void newStack(Stack* stack){
stack = (Stack*)malloc(sizeof(Stack));
stack->firstOut = NULL;
stack->size = 0;
}
void freeStack(Stack** stack){
empty((*stack));
free((*stack)->top);
(*stack)->size = 0;
free(stack);
}
My question is: are they well done? How would someone with proper experience do it?
newStack should simply return the stack pointer that it allocates. It doesn't need to receive a Stack* as an argument.
stack *newStack() {
Stack *stack = malloc(sizeof(Stack));
if (stack != NULL) { // allocation successful
stack->firstOut = NULL;
stack->size = 0;
}
return stack;
}
and freeStack should receive a stack pointer as argument, it doesn't need double indirection.
void freeStack(Stack *stack) {
empty(stack);
free(stack->firstOut); // This isn't done by empty()?
free(stack);
}
There's no need to set stack->size before freeing, since the memory is going to go away and the value it contains is irrelevant.
I am studying the following C code:
typedef struct msg *m_;
struct msg
{
long from;
long to;
m_ link;
};
m_ queue;
I would like to see an example that explains the role of the pointer, i.e. m_, of the structure inside the structure itself m_ link!
Thank you very much.
To be pedantic: link is a pointer. m_ is not a pointer, it's a typedef. It is used to avoid the need to say "struct msg* link;" inside the struct definition.
As answered in the comment above, the queue is represented by a pointer to the first item, which has a pointer to the second (if any), and so on until you reach a NULL pointer.
It's important to take care when building such lists that no node points to itself or to any precursor, or you get an infinite loop chasing to the tail.
Pointers to the structure type inside the structure itself are very often used for linked lists, trees, etc. In your example, it is referring to a queue implementation.
Here is a very minimal example of a stack implementation using a linked list. The functions require the address of a stack pointer, and an empty stack is a NULL pointer.
struct linked_stack
{
int data;
struct linked_stack *next;
};
void linked_stack_push(linked_stack **stck, int data)
{
struct linked_stack *node = malloc(sizeof(struct linked_stack));
if (node != NULL)
{
node->data = data;
node->next = *stck;
}
*stck = node;
}
int linked_stack_top(linked_stack **stck)
{
if (*stck != NULL)
return (*stck)->data;
return 0; /* stack is empty */
}
void linked_stack_pop(linked_stack **stck)
{
struct linked_stack *node = *stck;
if (*stck != NULL)
{
*stck = node->next;
free(node);
}
}
Example usage:
int main(void)
{
struct linked_stack *stack = NULL;
linked_stack_push(&stack, 10);
printf("top of stack = %d\n", linked_stack_top(&stack));
linked_stack_pop(&stack);
return 0;
}
I am currently working on stacks right now. I am supposed to use the following structures and function prototypes:
typedef struct node_{
char data;
struct node_ *next;
}node;
typedef struct stack_{
unsigned int size;
node* stack;
}stack;
stack* create_stack();
void push(stack* s, char val);
Here is my actual code for create_stack() and push():
stack* create_stack()
{
stack *stack;
stack = malloc(sizeof(stack));
stack->size = 0;
stack->stack = NULL;
return stack;
}
void push(stack* s, char val)
{
stack *newStack;
newStack = create_stack();
newStack->stack->data = val;
newStack->stack = s->stack;
s = newStack;
}
I am getting a segmentation fault when I try to store char val into newStack->stack->data. How does this not work? What do I need to do to make this stack on top???
The push function is wrong.
void push(stack* s, char val)
{
stack *newStack;
newStack = create_stack(); /* new stack created, why not work on the existing one ? */
newStack->stack->data = val; /* you're writing to a NULL pointer */
newStack->stack = s->stack;
s = newStack; /* this will not be visible from outside the function */
}
First of all, you are trying to recreate a new stack for each call of this function, which is certainly not what is intended.
If you try to modify the value of s, it will not be visible from outside the function, and you will still have your original stack.
Then, you are accessing the stack->data member even though stack has no space allocated to it yet (because you set it to NULL). You actually set it right after, which is why it crashes, most probably.
You probably want to do something like this:
void push(stack* s, char val)
{
node * n;
/* go to the end of the "stack" */
n = s->stack;
while (n != NULL) {
n = n->next;
}
/* allocate memory for a new node */
n = malloc(sizeof(node));
/* initialize node */
n->data = val;
n->next = NULL;
/* increment stack size */
s->size++;
}
And as mentionned before, this is merely a singly-linked list which is not the best fit for a stack, because as it exists now, you have to follow the node pointers to reach the last element, which makes push and pop operations O(N).
A faster implementation would look like this:
void push(stack* s, char val)
{
node * first_node, * new_node;
first_node = s->stack;
/* allocate memory for a new node */
new_node = malloc(sizeof(node));
/* initialize node */
new_node->data = val;
new_node->next = first_node;
/* increment stack size */
s->stack = new_node;
s->size++;
}
The top of the stack is always the first node, and the performance is O(1).
Follow your code....
stack *newStack = create_stack(); // in push()
newStack = malloc(sizeof(stack)); // in create_stack()
newStack->stack = NULL; // in create_stack()
newStack->stack->data = val; // in push()... this is where you crash.
Because newStack->stack is a NULL pointer. Your create_stack() function sets it to NULL, and you then dereference it. You have to allocate a struct node somewhere.
This code also has some readability issues which might be contributing to the problem. You are naming variables the same names as their types, which is very confusing. Consider using some other naming pattern like stack_t for types and stack for variable names.