Today I want to achieve the stack structure by using Objective-C Language.
First we need to define stack in a stack.h
As we can see in this file:
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;//Create a stack
//-(int)isEmpty:(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 detail,you can see many explanation in my code and Stack.m.
So let we can see some details in Stack.m:
Stack.m
#import <Foundation/Foundation.h>
#import "Stack.h"
@implementation Stack_Node
-(LinkStack*)CreateStack{//Create a new Stack.
LinkStack *S;
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)isEmpty:(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 (isEmpty(S)) {
// 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(@"%4d",topLink->Data);
topLink = topLink->next;
}
NSLog(@"\n");
}
-(unsigned int)Size{
return size;
}
@end
So we can see the main frame for my project.We can see the main.m 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];//initilize a interface for this stack.
[stack Push:(__bridge LinkStack *)(stack) andItem:10];
[stack Push:(__bridge LinkStack *)(stack) andItem:20];
[stack print];
[stack Push:(__bridge LinkStack *)(stack) andItem:5];
[stack Push:(__bridge LinkStack *)(stack) andItem:6];
[stack print];
[stack Pop:(__bridge LinkStack *)(stack)];
[stack print];
return 0;
}
程序的结果: