链表的object-c语言实现链表的创建、插入、删除、查找等功能(arc):
#import <UIKit/UIKit.h>
@interface List : NSObject//List为链表接口名字
@property NSInteger value;//节点整形成员
@property List *next;//节点指针型成员
//create
- (instancetype)init;//初始化链表
+(instancetype)createLikedList;//创建链表
-(void)insertBeforeList:(List *)Node inserbefore: (List *)NewNode;//在节点前插入节点
-(void)insertAfterList:(List *)NewNode;//在节点后插入节点
-(void)deleteList:(List *)Node;//删除节点
-(List *)searchList:(int)values;//查询节点
@end
#import "List.h"
@implementation List
- (instancetype)init{
if (self = [super init]) {//初始化链表
self->_value = 0;
self->_next = nil;
}
return self;
}
+ (instancetype)createLikedList {//创建链表
List *L = [[List alloc] init];
return L;
}
-(void)insertBeforeList:(List *)Node inserbefore: (List *)NewNode{//向前插入节点
List *CosorNode = self;
while (CosorNode.next != nil) {
if (CosorNode.next.value == Node.value) {//根据节点的value进行查询
NewNode.next = CosorNode.next;
CosorNode.next = NewNode;
break;
}else {
CosorNode = CosorNode.next;
}
}
}
-(void)insertAfterList:(List *)NewNode{//向后插入节点
List *CosorNode = self;
while (CosorNode.next != nil) {
CosorNode = CosorNode.next;
}
CosorNode.next =NewNode;
NewNode.next = nil;
}
-(void)deleteList:(List *)Node{//删除节点
List *CosorNode = self;
while (CosorNode.next!=nil) {
if (CosorNode.next.value == Node.value) {
CosorNode.next = Node.next;
break;
}else{
CosorNode = CosorNode.next;
}
}
}
-(List *)searchList:(int)values{//查找节点
List *CosorNode = self;
while (CosorNode.next!=nil) {
if (CosorNode.next.value == values) {
return CosorNode.next;
break;
}else{
CosorNode = CosorNode.next;
}
}
return nil;
}
@end
队列的object-c实现队列的创建、入队、出队功能(arc):
#import <Foundation/Foundation.h>
@interface Queue : NSObject
@property NSInteger value;
@property Queue *next;
-(instancetype)init;
+(instancetype)createWithQueue;
-(void)enqueue:(Queue *)Node;
-(instancetype)outqueue;
@end
#import "Queue.h"
@implementation Queue
-(instancetype)init{//初始化头结点
if (self = [super init]) {
self->_value = 0;
self->_next = nil;
}
return self;
}
+(instancetype)createWithQueue{
Queue *q = [[Queue alloc] init];//创建队列
return q;
}
-(void)enqueue:(Queue *)Node {//入队
Queue *q = self;
while (q.next!=nil) {//循环找到队列的最后一个节点(指向nil)。
q = q.next;
}
q.next = Node;//将尾节点指向入队节点
Node.next = nil;
}
-(instancetype)outqueue{//出队
Queue *q1;//作为转换的中间节点
Queue *q = self;
if (q.next==nil) {//如果队列只有一个则返回本身
return q;
}else{//否则取头结点后面的第一个节点作为返回值
q1 = q.next;
q.next = q.next.next;
return q1;
}
}
@end