Stack & Queue Implementation in Objective C

Stack & Queue Implementation in Objective C

Created By: Debasis Das (1-Oct-2015)

In this article we will create two sample implementation for a stack and a queue in objective C.

//  KSStack.h
//  StackAndQueue
//  Created by Debasis Das on 10/1/15.
//  Copyright © 2015 Knowstack. All rights reserved.

#import <Foundation/Foundation.h>

@interface KSStack : NSObject
@property (assign,readonly) long count;

-(void)push:(id)anObject;
-(id)pop;
-(void)clear;
-(id)lastObject;

@end
//  KSStack.m
//  StackAndQueue
//  Created by Debasis Das on 10/1/15.
//  Copyright © 2015 Knowstack. All rights reserved.

#import "KSStack.h"

@interface KSStack()
@property (strong) NSMutableArray *data;
@end

@implementation KSStack

-(id)init{
    if (self=[super init]){
        _data = [[NSMutableArray alloc] init];
        _count = 0;
    }
    return  self;
}

-(void)push:(id)anObject
{
    [self.data addObject:anObject];
    _count++;
}

-(id)pop{
    id obj = nil;
    if ([self.data count] > 0){
        obj = [self.data lastObject];
        [self.data removeLastObject];
        _count = self.data.count;
    }
    return obj;
}

-(void)clear{
    [self.data removeAllObjects];
    _count = 0;
}

-(id)lastObject{
    id obj = nil;
    if ([self.data count] > 0){
        obj = [self.data lastObject];
    }
    return obj;
}

@end

//  KSQueue.h
//  StackAndQueue
//  Created by Debasis Das on 10/1/15.
//  Copyright © 2015 Knowstack. All rights reserved.

#import <Foundation/Foundation.h>

@interface KSQueue : NSObject


-(void)enqueue:(id)anObject;
-(id)dequeue;

@end
//  KSQueue.m
//  StackAndQueue
//  Created by Debasis Das on 10/1/15.
//  Copyright © 2015 Knowstack. All rights reserved.

#import "KSQueue.h"
@interface KSQueue()
@property (strong) NSMutableArray *data;
@end

@implementation KSQueue
-(instancetype)init{
    if (self = [super init]){
        _data = [[NSMutableArray alloc] init];
    }
    return self;
}

-(void)enqueue:(id)anObject{
    [self.data addObject:anObject];
}

-(id)dequeue{
    id headObject = [self.data objectAtIndex:0];
    if (headObject != nil) {
        [self.data removeObjectAtIndex:0];
    }
    return headObject;
}

@end

Testing Code

-(void)testStack
{
    KSStack *stack = [[KSStack alloc] init];
    [stack push:@"John"];
    [stack push:@"Jane"];
    [stack push:@"Mary"];
    [stack push:@"Aby"];
    
    NSLog(@"stack %@",stack); 
    
    NSLog(@"%ld",stack.count); //4
    
    NSLog(@"Last Name before pop = %@",stack.lastObject); //Last Name before pop = Aby
    
    NSString *poppedName = [stack pop];
    
    NSLog(@"poppedName =  %@",poppedName); //poppedName =  Aby
    
    NSLog(@"New Stack Count = %ld",stack.count); //New Stack Count = 3

    NSLog(@"Last Name after pop = %@",stack.lastObject); //Last Name after pop = Mary
    
    //Clear the stack
    [stack clear];
    
    NSLog(@"Stack Count after clear = %ld",stack.count); //Stack Count after clear = 0
}

-(void)testQueue{
    
    KSQueue *queue = [[KSQueue alloc] init];
    [queue enqueue:@"Jim"];
    [queue enqueue:@"John"];
    [queue enqueue:@"Mary"];

    //Follows First In First Out
    NSLog(@"%@",queue.dequeue); //Jim
    NSLog(@"%@",queue.dequeue); //John
    NSLog(@"%@",queue.dequeue); //Mary
}
Posted in Cocoa, Cocoa Touch, Objective C Tagged with: , , ,

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

Recent Posts


Hit Counter provided by technology news