您的位置:首页 > 移动开发 > Objective-C

Objective-C achieve Queue using Two Stacks(用两个堆栈实现队列)

2015-04-26 11:03 549 查看

introduction

Today I want to achieve the Queue using stacks structure in Objective-C Language.

We can create a new objective-C console program to achieve this.So let us see the structure of this project :



First we need to define stack in a stack.h which are the definition of the stacks:

As we can see in this file.

Codes

Stack.h:

#ifndef Using_Stack_Achieve_Queue_Stack_h
#endif
#define Using_Stack_Achieve_Queue_Stack_h
#define ElementType int
typedef struct Node{//Initilize the Stack structure.
ElementType Data;//Data area.
struct Node *next;//Next point area.
}LinkStack;
//LinkStack *Top;
//I need to pay attention to this error.Don't define a variate twice using same name in the same class(interface).
@interface Stack_Node : NSObject{//Class for stack.
LinkStack *Top;//Initilize a Top point which server for finding item form the stack.
unsigned int size;//Record the size of the stack(item counter) as well as using as a flag to judge the stack whether is empty in Pop method.
}

-(LinkStack*)CreateStack:(LinkStack*)S;//Create a stack
//-(int)StackEmpty:(LinkStack*)S;//Cheack the stack S wheather it is Empty
-(void)Push:(LinkStack*)S andItem:(ElementType)item;//Push the last element into the Stack
-(ElementType)Pop:(LinkStack*)S;//Pop(Delect) the first element out of the Stack;
-(void)print;//print the result in console.
-(unsigned int)Size;//return the size of the stack.

@end


As we can see that there are many methods we need to achieve just like:CreateStack、Push、Pop、print、Size.If you want to know some details,you can see many explanation in my code and Stack.m.

So let we see some details in Stack.m:

#import <Foundation/Foundation.h>
#import "Stack.h"

@implementation Stack_Node

-(LinkStack*)CreateStack:(LinkStack*)S{//Create a new Stack.
S = malloc(sizeof(LinkStack));//Open a momery for a new item.
S->next = NULL;//Make a empty stack.
size = 0;//item counter.
return S;
}

//-(int)StackEmpty:(LinkStack*)S{
//    return (S->next == NULL);
//}

-(void)Push:(LinkStack*)S andItem:(int)item{
LinkStack *TmpCell;
//create a new momery space for a temp item.
TmpCell = malloc(sizeof(LinkStack));
if (TmpCell == 0)
{
fprintf(stderr, "Out of memory\n");
return;
}
//If momery is full , printf error and break;
TmpCell->Data = item;//insert the item into the stack.
TmpCell->next = S->next;
S->next = TmpCell;
size++;//counter++;
}
-(ElementType)Pop:(LinkStack *)S{//Pop the first item
LinkStack *FirstCell;//deal with the firstitem.
ElementType TopElem;
//    if (StackEmpty(S)) {
//        NSLog(@"堆栈空");
//        return 0;
//    }
if (S->next == NULL) {
NSLog(@"堆栈空");
return 0;
}
//    if (size == 0) {//if the stack is empty.
//        return 0;
//    }
else{
FirstCell = S->next;
S->next = FirstCell->next;
TopElem = FirstCell->Data;
free(FirstCell);//free the momery.
size--;
return TopElem;
}
}
-(void)print{//print all items in console.
LinkStack *topLink;
topLink = Top;
while (topLink) {
NSLog(@"%d",topLink->Data);
topLink = topLink->next;
}
NSLog(@"\n");
}

-(unsigned int)Size{
return size;
}

@end


Main Function:

We have defined the stack in my head file and objective-C file,so we need to use them to achieve our function which is to achieve a queue.As we all know ,there is a most important feature for queue that is the “FIFO”-“First In First Out”.We also know that the most important feature for stack is “FILO”-“First In Last Out”.

So if we want to use two stacks to achieve a queue , we can use the first stack to get and save elements which we wait for inputing as well as we can use the other one stack to output those elements to achieve the queue.

So let we see the main.m file to show this:

main.m

#import <Foundation/Foundation.h>
#import "Stack.h"

int main(int argc, char * argv[]){
//    Stack_Node *S1,*S2;
//    LinkStack *Stack1,*Stack2;
//    for (int i = 0; i < 5; i++) {
//
//    }
id stack;//get a new stack.
stack = [Stack_Node new];//initialize a interface for this stack.
//initialize two stacks.
LinkStack *S1,*S2;
S1 = [stack CreateStack:S1];
S2 = [stack CreateStack:S2];
//Push some elements into the first stack for testing this program.
for (int i = 0; i < 10; i++) {
[stack Push:S1 andItem:i];
}
//Push stack1's elements into the stack2 so the order of the stack2 is just like a queue's "FIFO".
while (S1->next != NULL) {
[stack Push:S2 andItem:[stack Pop:S1]];
//        S1 = S1->next;
}
//Output all the items in stack2 to show the result of this program.
while (S2->next != NULL) {
NSLog(@"%d",[stack Pop:S2]);
//        S2 = S2->next;
}
return 0;
}


There are some tips we need to pay attention to:

When we are initializing the stacks,we need to create two containers to accept the address of new stacks.If we missed it , there are some memory errors appear in our program just like using NULL point NULL. Just like there :

LinkStack *S1,*S2;
S1 = [stack CreateStack:S1];
S2 = [stack CreateStack:S2];


We need to focus on the condition for the loop we want solve the stacks.As we can see this :

for (int i = 0; i < 10; i++) {
[stack Push:S1 andItem:i];
}
//Push stack1's elements into the stack2 so the order of the stack2 is just like a queue's "FIFO".
while (S1->next != NULL) {
[stack Push:S2 andItem:[stack Pop:S1]];
//        S1 = S1->next;
}
//Output all the items in stack2 to show the result of this program.
while (S2->next != NULL) {
NSLog(@"%d",[stack Pop:S2]);
//        S2 = S2->next;
}


Results:

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: