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:
相关文章推荐
- [CareerCup] 3.5 Implement Queue using Two Stacks 使用两个栈来实现队列
- LeetCode学习之-232. 利用堆栈实现队列(Implement Queue using Stacks)
- leetcode:用两个栈实现一个队列 Implement Queue using Stacks
- Implement Queue using Stacks(用两个栈实现队列)
- [LeetCode-232] Implement Queue using Stacks(两个栈实现一个队列)
- Implement Queue using Stacks 两个栈实现队列
- Leetcode232. Implement Queue using Stacks[栈实现队列]
- Implement queue with two stack 用两个堆实现队列
- LeetCode(23)-Implement Queue using Stacks(栈实现队列)
- LeetCode(23)-Implement Queue using Stacks(栈实现队列)
- LeetCode 232 Implement Queue using Stacks(用栈来实现队列)(*)
- Implement Queue using Stacks(用栈实现队列)
- lintcode :implement queue by two stacks 用栈实现队列
- 队列-Implement Queue using Stacks(用栈实现队列)
- 18.用两个栈实现队列[2StacksToImplementQueue]
- 232.Implement Queue using Stacks(用栈实现队列)
- (LeetCode)Implement Queue using Stacks --- 实现队列和栈第一部分,队列
- LeetCode 232. Implement Queue using Stacks(用栈实现队列)
- [LeetCode] Implement Queue using Stacks 用栈来实现队列
- 【LeetCode 232_数据结构_队列_实现】Implement Queue using Stacks