Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

[pull] master from youngyangyang04:master #521

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
pull merged 6 commits into AlgorithmAndLeetCode:master from youngyangyang04:master
Jan 8, 2025
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Prev Previous commit
Next Next commit
添加 0225.用队列实现栈.md C 版本
  • Loading branch information
c-qwer committed Dec 12, 2024
commit d6f7f3adbcd2532fafe6ffc06efc4e3d01f8d1ee
89 changes: 89 additions & 0 deletions problems/0225.用队列实现栈.md
View file Open in desktop
Original file line number Diff line number Diff line change
Expand Up @@ -1277,6 +1277,95 @@ impl MyStack {
}
```

### C:

> C:单队列

```c
typedef struct Node {
int val;
struct Node *next;
} Node_t;

// 用单向链表实现queue
typedef struct {
Node_t *head;
Node_t *foot;
int size;
} MyStack;

MyStack* myStackCreate() {
MyStack *obj = (MyStack *)malloc(sizeof(MyStack));
assert(obj);
obj->head = NULL;
obj->foot = NULL;
obj->size = 0;
return obj;
}

void myStackPush(MyStack* obj, int x) {

Node_t *temp = (Node_t *)malloc(sizeof(Node_t));
assert(temp);
temp->val = x;
temp->next = NULL;

// 添加至queue末尾
if (obj->foot) {
obj->foot->next = temp;
} else {
obj->head = temp;
}
obj->foot = temp;
obj->size++;
}

int myStackPop(MyStack* obj) {

// 获取末尾元素
int target = obj->foot->val;

if (obj->head == obj->foot) {
free(obj->foot);
obj->head = NULL;
obj->foot = NULL;
} else {

Node_t *prev = obj->head;
// 移动至queue尾部节点前一个节点
while (prev->next != obj->foot) {
prev = prev->next;
}

free(obj->foot);
obj->foot = prev;
obj->foot->next = NULL;
}

obj->size--;
return target;
}

int myStackTop(MyStack* obj) {
return obj->foot->val;
}

bool myStackEmpty(MyStack* obj) {
return obj->size == 0;
}

void myStackFree(MyStack* obj) {
Node_t *curr = obj->head;
while (curr != NULL) {
Node_t *temp = curr->next;
free(curr);
curr = temp;
}
free(obj);
}

```


<p align="center">
<a href="https://programmercarl.com/other/kstar.html" target="_blank">
Expand Down

AltStyle によって変換されたページ (->オリジナル) /