Skip to content

Instantly share code, notes, and snippets.

@kmdarshan
Created February 28, 2015 23:21
Show Gist options
  • Save kmdarshan/7d2b66d12bf013b28480 to your computer and use it in GitHub Desktop.
Save kmdarshan/7d2b66d12bf013b28480 to your computer and use it in GitHub Desktop.
Circular linked list in Objective C
//
// 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