【问题标题】:How to reverse single link list , using Objective C如何使用Objective C反转单链表
【发布时间】:2018-03-16 03:59:25
【问题描述】:

我写了一个单链表,但是不知道怎么反转,这是我的一些代码。

节点类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...?
}

【问题讨论】:

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


    【解决方案1】:

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

    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
    

    SinglyLinkedList.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这个链接

    【讨论】:

    • 谢谢!得到它。
    • @MorrisMeng :如果它解决了您的问题,那么您可以将答案标记为已接受。所以其他人也可以发现它很有用。谢谢
    猜你喜欢
    • 2016-04-22
    • 2012-01-30
    • 2016-03-19
    • 1970-01-01
    • 1970-01-01
    • 2018-11-16
    • 2016-09-29
    相关资源
    最近更新 更多