Thông tin

Tác giả Maxime Mawait & Nicolas Rybowski
Hạn chót Không có hạn chót
Giới hạn nộp bài Không có giới hạn
Các tag chuyên mục Struct, S4, Malloc, Level 4, Pointer, Linked list

Tags

Đăng nhập

[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.

Câu hỏi 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){
Câu hỏi 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){