[Data structure] stack-array implementation, data structure array implementation

Source: Internet
Author: User

[Data structure] stack-array implementation, data structure array implementation

The first is to define the basic structure of the stack, because the array is used to implement

private String[] stack;private int TOP = 0;

Then the constructor

StackOfStrings(int capacity) {stack = new String[capacity];}

Next, push. Note that TOP always points to the last position of the pressed element.

public void push(String str) {if (TOP == stack.length) // if full, increase the stack sizeresize(2 * stack.length);stack[TOP++] = str;}

Then pop

public String pop() {String popItem = stack[--TOP];// if equal to 1/4, decrease the stack sizeif (TOP > 0 && TOP == stack.length / 4)resize(stack.length / 2);return popItem;}


Then, I am trying to determine the null operation. I want to see why TOP 0 is empty.

public boolean isEmpty() {return TOP == 0;}

The code for resize is attached.

public void resize(int capacity) {String[] temp = new String[capacity];for (int i = 0; i < TOP; i++)temp[i] = stack[i];stack = temp;}





Use arrays to implement the data structure of stacks and queues

# Include <stdlib. h>
# Include <stdio. h>

Class Stack
{
Private:
Int ele [100];
Int top;
Public:
Stack (){
Top = 0;
}
Void push (int v ){
Ele [top ++] = v;
}
Int pop (){
If (isEmpty ()){
Printf ("The stack is Empty. \ n ");
Return-1;
}
Return ele [-- top];
}
Bool isEmpty (){
Return top = 0;
}
~ Stack (){
}
};

# Define SIZE 100
Class Queue
{
Private:
Int ele [SIZE];
Int head;
Int tail;
Public:
Queue (){
Head = 0;
Tail = 0;
}
Bool isFull (){
Return (tail + 1) % SIZE = head;
}
Bool isEmpty (){
Return head = tail;
}
Void enqueue (int val ){
If (isFull ()){
Printf ("The queue is full, can not enqueue for: % d \ n", val );
Return;
}
Ele [tail] = val;
Tail = (tail + 1) % SIZE;
}
Int dequeue (){
If (isEmpty ()){
Printf ("The queue is empty, can not dequeue. \ n ");
Return-1;
}
Int value = ele [head];
Head = (head + 1) % SIZE;
Return value;
}
};

Int main ()
{
Stack s;
S. push (1, 123 );
S. push (12 );
While (! S. isEmpty ()){
Printf ("% d", s. pop ());
}
Printf ("\ n ");

Queue q;
Q. enqueue (88 );
Q. enqueue (13 );
Q. enqueue (123 );
While (! Q. isEmpty () printf ("% d", q. dequeue ());
Printf ("\ n ");
System ("pause ");
}... Remaining full text>

The data structure uses arrays to implement the stack. below is the structure given in the book, but what type is ElementType? Not written in the book

It can be of any type. You can define one by yourself.
For example:
Typedef ElementType int; // ElementType * Array; it indicates int * Array; now, you can operate Array by int Array. Of course, the premise is that you need to apply for a pointer space in your code.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.