Using Objective-C achieve the stack structure.(用Objective-C实现堆栈)
来源:互联网 发布:java无参方法构造 编辑:程序博客网 时间:2024/05/01 07:34
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 inttypedef 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;}程序的结果:
0 0
- Using Objective-C achieve the stack structure.(用Objective-C实现堆栈)
- Objective-C achieve Queue using Two Stacks(用两个堆栈实现队列)
- Using C++ With Objective-C
- Using C++ With Objective-C
- Using C++ With Objective-C
- Using C++ With Objective-C
- Using Alamofire from Objective-C
- Objective-C 队列实现
- objective-c单例类实现
- objective-c单例类实现
- Objective-C底层实现
- Objective-C’s Roots: Difference Between messaging structure and function calling; and the Runtime
- Stack and Heap Objects in Objective-C
- Understanding the Objective-C Runtime
- Inside the Objective-C Runtime
- Understanding the Objective-C Runtime
- Understanding the Objective-C Runtime
- Understanding the Objective-C Runtime
- Android一键锁屏程序实例
- 杭电 HDU 1215 七夕节
- Protocol Layers in Computer Network
- oc 的第四天
- .Net自定义控件之INamingContainer接口详解
- Using Objective-C achieve the stack structure.(用Objective-C实现堆栈)
- UVA - 12589(learning vector dp)
- android 输入框 XML 设置于代码设置异同
- 海盗分宝石
- GetFileName()有效最大文件长度不是1085
- iOS集成构建总结 (libimobiledevice)
- Stanford机器学习课程笔记2-高斯判别分析与朴素贝叶斯
- 在创建触发器时出现不能在 'inserted' 表和 'deleted' 表中使用 text、ntext 或 image 列
- ClearEditText