Created
February 28, 2015 23:21
-
-
Save kmdarshan/7d2b66d12bf013b28480 to your computer and use it in GitHub Desktop.
Circular linked list in Objective C
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// CircularLinkedList.h | |
// learnObjectiveC | |
// | |
// Created by kmd on 2/28/15. | |
// Copyright (c) 2015 Happy Days. All rights reserved. | |
// | |
#import <Foundation/Foundation.h> | |
@interface CircularLinkedList : NSObject | |
+(CircularLinkedList*)initialize; | |
-(void) insertFront:(NSInteger)value; | |
-(void) displayNodes; | |
-(void) deleteFrontNode; | |
@end | |
// | |
// CircularLinkedList.m | |
// learnObjectiveC | |
// | |
// Created by kmd on 2/28/15. | |
// Copyright (c) 2015 Happy Days. All rights reserved. | |
// | |
#import "CircularLinkedList.h" | |
@interface CNode : NSObject | |
@property (nonatomic, strong) CNode *link; | |
@property (nonatomic, strong) id data; | |
@end | |
@implementation CNode | |
@end | |
@interface CircularLinkedList () | |
@property (nonatomic, strong) CNode* last; | |
@property (nonatomic, strong) CNode* first; | |
@end | |
@implementation CircularLinkedList | |
+(CircularLinkedList*)initialize { | |
static CircularLinkedList *circularLinkedList; | |
if (!circularLinkedList) { | |
circularLinkedList = [CircularLinkedList new]; | |
} | |
return circularLinkedList; | |
} | |
-(void) insertFront:(NSInteger)value { | |
CNode* temp = [CNode new]; | |
temp.data = [NSNumber numberWithInteger:value]; | |
if (self.first == nil && self.last == nil) { | |
self.first = temp; | |
self.last = temp; | |
} else { | |
CNode *tail = self.last; | |
tail.link = temp; | |
temp.link = self.first; | |
self.last = temp; | |
} | |
} | |
-(void) deleteFrontNode { | |
if (self.last == nil && self.first == nil) { | |
NSLog(@"nothing to delete"); | |
} else { | |
self.last.link = self.first.link; | |
} | |
} | |
-(void) displayNodes { | |
CNode *temp; | |
if (self.last == NULL) { | |
NSLog(@"empty"); | |
} else { | |
for (temp = self.last.link; temp!= self.last; temp=temp.link) { | |
NSLog(@"%@", temp.data); | |
} | |
NSLog(@"%@", temp.data); | |
} | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment