objective-c - 如何使用 Objective C 反转单链表

标签 objective-c algorithm data-structures linked-list singly-linked-list

我写了一个单链接列表,但不知道如何反转,这是我的一些代码。

节点类MMNode

 #import <Foundation/Foundation.h>

 @interface MMNode : NSObject

 @property (nonatomic, assign) int data;//data
 @property (nonatomic, strong) MMNode *next;//next node

 @end

单链表类MMList

  #import <Foundation/Foundation.h>
  #import "MMNode.h"

  @interface MMList : NSObject

  @property (nonatomic, strong) MMNode *head;//first node

  @property (nonatomic, strong) MMNode *hail;//last node

 //init
 - (instancetype)initWithData:(int)data;

 //append
 - (void)append:(int)data;

//print
- (void)printList;

//
- (void)reverse;

@end

如何反转单链表?

- (void)reverse {

   //Code...?
}

最佳答案

我正在发布我的代码,因为我不知道你到底实现了什么。

LinkNode.h

#import <Foundation/Foundation.h>

@interface LinkNode : NSObject
@property (nonatomic) int data;
@property (nonatomic) LinkNode *next;

-(id)initWithData:(int) data;

@end

LinkNode.m

#import "LinkNode.h"

@implementation LinkNode

-(id)initWithData:(int) data{

    if (self = [super init]){
        self.data = data;
        self.next = nil;
    }
    return self;
}

@end

SinglyLinkedList.h

#import <Foundation/Foundation.h>
#import "LinkNode.h"

@interface SinglyLinkedList : NSObject

@property(nonatomic) LinkNode *head;
@property(nonatomic) LinkNode *current;

-(id) initWithCapacity: (int)capacity;
-(void) appendData:(int)data;
-(void) prependData : (int)data;
-(void) printList:(LinkNode*)node;
-(void) reverseList;
-(void) deleteData:(int)data;

@end

单链表.m

#import "SinglyLinkedList.h"
#import "LinkNode.h"

@interface SinglyLinkedList()

@end

@implementation SinglyLinkedList

    -(id)initWithCapacity:(int)capacity{

    if (self == [super init]) {
        for (int i=1; i<=capacity;i++) {
            [self appendData:i];
        }
    }
    return self;
}

-(void) appendData:(int)data{

    if (self.head == nil){
        self.current = [[LinkNode alloc] initWithData:data];
        self.head = self.current;
        return;
    }

    while (self.current.next != nil) {
        self.current = self.current.next;
    }

    self.current.next = [[LinkNode alloc] initWithData:data];
}

-(void)prependData:(int)data{

    if (self.head == nil){
        self.current = [[LinkNode alloc] initWithData:data];
        self.head = self.current;
        return;
    }

    self.current = [[LinkNode alloc] initWithData:data];
    self.current.next = self.head;
    self.head = self.current;
}

-(void) printList:(LinkNode *)node {

    while (node != nil) {
        NSLog(@" %d \t", node.data);
        node = node.next;
    }
}

-(void) reverseList {

    LinkNode *prev = nil;
    LinkNode *current = self.head;
    LinkNode *next = nil;

    while (current != nil) {
        next = current.next;
        current.next = prev;
        prev = current;
        current = next;
    }
    self.head = prev;
}

-(void)deleteData:(int)data{

    if (self.head == nil) { return; }

    if (self.head.data == data) {
        self.head = self.head.next;
        return;
    }

    LinkNode *currentNode = self.head;
    while (currentNode.next != nil) {
        if (currentNode.next.data == data){
            currentNode.next = currentNode.next.next;
            return;
        }
        currentNode = currentNode.next;
    }
}

@end

main.m调用

int main(int argc, const char * argv[]) {

    SinglyLinkedList *list = [[SinglyLinkedList alloc] initWithCapacity:5];
    [list appendData:15];
    NSLog(@"After Append");
    [list printList:list.head];
    [list prependData:0];
    NSLog(@"After Prepend");
    [list printList:list.head];
    [list reverseList];
    NSLog(@"After Reverse");
    [list printList:list.head];
    [list deleteData:15];
    [list reverseList];
    NSLog(@"After Delete");
    [list printList:list.head];
    return NSApplicationMain(argc, argv);

}

要了解更多信息,您可以引用来自 geeksforgeeks 的链接。

关于objective-c - 如何使用 Objective C 反转单链表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49312985/

相关文章:

algorithm - 有向图中的顶点,存在从该顶点到每个其他顶点的路径

algorithm - Boggle - 计算 N*N 网格上的所有可能路径。表现

ios - UIViewController 的循环展示

iphone - __weak UIDataType *weakSelf 和 UIDataType __weak *weakSelf 之间的区别?

objective-c - 如何以编程方式启动 NSTextFieldCell 编辑(使其成为第一个Responder)

python - 空字典 Python 中的 KeyError

algorithm - 数据结构预处理给定的 N 点集并给定查询并行带输出所有点都位于带内

performance - 在 Redis 中创建中型到大型列表/集合/zset/哈希的最有效方法是什么?

c++ - 二叉树 getParent 函数

iphone - 在 iOS 中组合图像时像素损坏