Information

Author(s) Maxime Mawait & Nicolas Rybowski
Deadline Καμία προθεσμία
Submission limit No limitation
Category tags Struct, S4, Malloc, Level 4, Pointer, Linked list

Ετικετές

Συνδεθείτε

[S4] Simple stack

You are asked to implement the pop and push functions of the following stack interface :

struct node {
    struct node *next;
    char *name;
};
https://upload.wikimedia.org/wikipedia/commons/b/b4/Lifo_stack.png

Hints :

  • char *name is also a pointer, memory must be allocated by using malloc(3) to copy the string on the stack.
  • Other useful commands: strncpy(3) and strlen(3).
  • Do not forget to free all the allocated space when popping one element.

Question 1: Pop
/**
* Remove the top element of the stack and return its content.
*
* @head : pointer to the top of the stack
* @result : pointer to store the popped element
*
* @return 0 if no error, 1 otherwise
*
* pre : @result contains a null-terminated correct string
* post : @result contains the string @name from the element at the top of the stack
*/

int pop(struct node **head, char *result){
Question 2: Push
/**
* Add @name at the "top" of the stack.
*
* @head : pointer to the top of the stack
* @name : the string to be placed in the element at the top of the stack
*
* @return 0 if no error, 1 otherwise
*/

int push(struct node **head, const char *value){