栈、队列、数组

定义

#include <stdio.h>

/*
栈
    只允许在栈顶插入删除操作的线性表
    Last Insert First Out.
*/

// 顺序栈

#define MaxSize 10

typedef struct 
{
    int data[MaxSize];  // 静态数组存放栈元素
    int top;  // 栈顶指针
} SqStack;

栈顶指针指向栈顶元素的栈 空为-1

// 栈顶指针指向栈顶元素 空为-1

void InitStack1(SqStack &S)
{
    S.top = -1;  // 栈空
}

void Push1(SqStack &S, int x)
{
    S.data[++S.top] = x;
}

int  Pop1(SqStack &S)
{   
    int x;
    x = S.data[S.top--];
    return x;
}

int GetTop1(SqStack S)
{
    return S.data[S.top];
}

测试

int main()
{
    SqStack S;
    InitStack1(S);

    for(int i=1; i<=5; i++)
    {
        Push1(S, i);
    }
    

    puts("after push...");
    show1(S);
    puts("pop....");
    while( S.top != -1)
    {
        int x = Pop1(S);
        printf("%i\n", x);
    }
    

    return 0;
}

void show1(SqStack S)
{
    for(int i=S.top; i!=-1; i--)
    {
        printf("| %i |\n", S.data[i]);
        puts("-----\n");
    }
         
}

image

栈顶指针指向下一个元素存放位置的栈 空为 0

// 栈顶指针指向下一个元素存放位置 空为 0

void InitStack0(SqStack &S)
{
    S.top = 0;  // 栈空
}

void Push0(SqStack &S, int x)
{
    S.data[S.top++] = x;
}

void Pop0(SqStack &S)
{
    int x;
    x = S.data[--S.top];
}

int GetTop0(SqStack S)
{
    int x;
    x = S.data[S.top - 1];
    return x;
}

共享栈

// 共享栈
typedef struct
{
    int data[MaxSize];
    int topa;  // 栈a 以下方为栈底 的栈顶指针
    int topb;  // 栈b 以上方位栈底 的栈顶指针
}ShStack;

void InitShStack(ShStack &S)
{
    S.topa = -1;
    S.topb = MaxSize;
}

bool isFull(ShStack S)  // 栈是否已满
{
    return S.topa + 1 == S.topb;
}

bool Pusha(ShStack &S, int x)
{
    if(isFull(S) == true)
    {
        return false;
    }
    S.data[++S.topa] = x;

    return true;
}

bool Pushb(ShStack &S, int x)
{
    if(isFull(S) == true)
    {
        return false;
    }
    S.data[++S.topb] = x;

    return true;
}

int Popa(ShStack &S)
{
    return S.data[S.topa--];
}

int Popb(ShStack &S)
{
    return S.data[S.topb--];
}

队列

数组

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
Java实现的代码: ```java public class Stack { private int maxSize; private int[] stackArray; private int top; public Stack(int size) { maxSize = size; stackArray = new int[maxSize]; top = -1; } public void push(int value) { if (isFull()) { System.out.println("Stack is full!"); return; } stackArray[++top] = value; } public int pop() { if (isEmpty()) { System.out.println("Stack is empty!"); return -1; } return stackArray[top--]; } public int peek() { if (isEmpty()) { System.out.println("Stack is empty!"); return -1; } return stackArray[top]; } public boolean isEmpty() { return (top == -1); } public boolean isFull() { return (top == maxSize - 1); } } ``` Java实现队列的代码: ```java public class Queue { private int maxSize; private int[] queueArray; private int front; private int rear; private int nItems; public Queue(int size) { maxSize = size; queueArray = new int[maxSize]; front = 0; rear = -1; nItems = 0; } public void insert(int value) { if (isFull()) { System.out.println("Queue is full!"); return; } if (rear == maxSize - 1) { rear = -1; } queueArray[++rear] = value; nItems++; } public int remove() { if (isEmpty()) { System.out.println("Queue is empty!"); return -1; } int temp = queueArray[front++]; if (front == maxSize) { front = 0; } nItems--; return temp; } public int peekFront() { if (isEmpty()) { System.out.println("Queue is empty!"); return -1; } return queueArray[front]; } public boolean isEmpty() { return (nItems == 0); } public boolean isFull() { return (nItems == maxSize); } } ``` Java实现数组的代码: ```java public class MyArray { private int[] array; private int length; public MyArray(int[] arr) { array = arr; length = arr.length; } public int get(int index) { if (index < 0 || index >= length) { System.out.println("Invalid index!"); return -1; } return array[index]; } public void set(int index, int value) { if (index < 0 || index >= length) { System.out.println("Invalid index!"); return; } array[index] = value; } public int length() { return length; } public void display() { for (int i = 0; i < length; i++) { System.out.print(array[i] + " "); } System.out.println(); } } ``` Java实现链表的代码: ```java public class Node { public int value; public Node next; public Node(int val) { value = val; next = null; } } public class LinkedList { private Node head; public LinkedList() { head = null; } public void insertFirst(int value) { Node newNode = new Node(value); newNode.next = head; head = newNode; } public Node deleteFirst() { if (isEmpty()) { System.out.println("List is empty!"); return null; } Node temp = head; head = head.next; return temp; } public boolean isEmpty() { return (head == null); } public void display() { Node current = head; while (current != null) { System.out.print(current.value + " "); current = current.next; } System.out.println(); } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

JFLEARN

CSDN这么拉会有人打赏?

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值