共享栈
#include <iostream>
using namespace std;
const int N = 100;
struct stack
{
int data[N];
int top[2];//top[0]为s[0]栈顶,top[1]为s[1]栈顶
};
int push(stack&st,int stno,int x)
{
if(st.top[0]+1<st.top[1])
{
if(stno==0)
{
st.top[0]++;
st.data[st.top[0]]=x;
return 1;
}
else if(stno==1)
{
st.top[1]--;
st.data[st.top[1]]=x;
return 1;
}
else return -1;//输入编号错误
}
return -1;//共享栈已满
}
int pop(stack&st,int stno)
{
if(stno==0)
{
//st[0]栈不为空
if(st.top[0]!=-1)
{
int x=st.data[st.top[0]];
st.top[0]--;
return x;
}
else return -1;
}
else if(stno==1)
{
if(st.top[1]!=N)
{
int x=st.data[st.top[1]];
st.top[1]++;
return x;
}
else return -1;
}
else return-1;
}
int main() {
stack st;
st.top[0] = -1; // 初始化s[0]栈顶指针
st.top[1] = N; // 初始化s[1]栈顶指针
// 测试s[0]栈的压栈操作
cout << "Pushing 1 to stack 0: " << push(st, 0, 1) << endl;
cout << "Pushing 2 to stack 0: " << push(st, 0, 2) << endl;
// 测试s[1]栈的压栈操作
cout << "Pushing 100 to stack 1: " << push(st, 1, 100) << endl;
cout << "Pushing 200 to stack 1: " << push(st, 1, 200) << endl;
// 测试s[0]栈的出栈操作
cout << "Popping from stack 0: " << pop(st, 0) << endl;
cout << "Popping from stack 0: " << pop(st, 0) << endl;
// 测试s[1]栈的出栈操作
cout << "Popping from stack 1: " << pop(st, 1) << endl;
cout << "Popping from stack 1: " << pop(st, 1) << endl;
}
第六题
#include <iostream>
using namespace std;
// 节点定义
struct Node {
int val;
Node *next;
Node(int x) : val(x), next(NULL) {}
};
Node *CreateListWithTail(int arr[], int n) {
Node*dummy=new Node(-1);
Node*tail=dummy;
for(int i=0;i<n;i++)
{
Node*node=new Node(arr[i]);
tail->next=node;
tail=node;
}
tail->next=dummy;
return tail;
}
void print(Node *tail)
{
Node*cur=tail->next->next;
while(cur!=tail->next)
{
cout<<cur->val<<" ";
cur=cur->next;
}
cout<<endl;
}
Node* insert_queue(Node*tail,int x)
{
Node *node=new Node(x);
node->next=tail->next;
tail->next=node;
tail=node;
}
Node* delete_queue(Node*tail)
{
if(tail->next==tail)return NULL;
Node*s=tail->next->next;
tail->next->next=s->next;
if(s==tail)tail=tail->next;//如果删除的是尾节点,tail指向头结点
return tail;
}
int main()
{
int arr[]={1,2,3,4,5,6};
int n=sizeof(arr)/sizeof(int);
Node *tail= CreateListWithTail(arr,n);
print(tail);
tail=delete_queue(tail);
print(tail);
}
第8题
#include <iostream>
using namespace std;
const int N = 10;
struct queue {
int data[N];
int front, rear;
int tag; // 0 for empty, 1 for full
};
void init(queue &q) {
q.front = q.rear = 0;
q.tag = 0;
}
int is_queue_empty(const queue &q) {
return (q.rear == q.front && q.tag == 0);
}
int is_queue_full(const queue &q) {
return (q.rear == q.front && q.tag == 1);
}
int enqueue(queue &q, int x) {
if (is_queue_full(q)) return 0; // Queue is full, cannot enqueue
q.data[q.rear] = x;
q.rear = (q.rear + 1) % N;
if (q.rear == q.front) q.tag = 1; // Queue is full
return 1;
}
int dequeue(queue &q, int &x) {
if (is_queue_empty(q)) return 0; // Queue is empty, cannot dequeue
x = q.data[q.front];
q.front = (q.front + 1) % N;
if (q.front == q.rear) q.tag = 0; // Queue is empty after dequeue
return 1;
}
int main() {
queue q;
init(q);
// Example usage
enqueue(q, 1);
enqueue(q, 2);
enqueue(q, 3);
int x;
while (!is_queue_empty(q)) {
dequeue(q, x);
cout << x << " ";
}
cout << endl;
return 0;
}
第九题
#include <iostream>
#include<stack>
using namespace std;
void reans(int n)
{
stack<int>st;
while(n!=0)
{
int k=n%2;
n=n/2;
st.push(k);
}
while(!st.empty())
{
cout<<st.top()<<" ";
st.pop();
}
cout<<endl;
}
int main() {
int n;
cin>>n;
reans(n);
}
判断单链表是否中心对称
#include <iostream>
#include <cstring>
#include <algorithm>
#include<stack>
using namespace std;
// 节点定义
struct Node {
char val;
Node *next;
Node(int x) : val(x), next(NULL) {}
};
Node *CreateListWithHead(char s[], int n) {
Node*dummy=new Node(-1);
for(int i=0;i<n;i++)
{
Node*node=new Node(s[i]);
node->next=dummy->next;
dummy->next=node;
}
return dummy->next;
}
void print(Node *head)
{
for (Node *p = head; p; p = p->next)
cout << p->val << ' ';
cout << endl;
}
void is_zhou(Node*head,char s[],int n)
{
stack<int>st;
Node*cur=head;
int count=0;
for(int i=0;i<n/2;i++)
{
st.push(cur->val);
cur=cur->next;
}
if(n%2==1)cur=cur->next;
while(cur)
{
if(cur->val!=st.top())
{
cout<<"非轴对称"<<endl;
return;
}
st.pop();
cur=cur->next;
}
cout<<"轴对称"<<endl;
}
int main() {
char s[]= "a" ;
int n = strlen(s);
Node*head=CreateListWithHead(s,n);
is_zhou(head,s,n);
}