如何使用 Objective C 反转单个 link 列表
How to reverse single link list , using Objective C
我写了一个 link 列表,但不知道如何反转,这是我的一些代码。
节点class MMNode
#import <Foundation/Foundation.h>
@interface MMNode : NSObject
@property (nonatomic, assign) int data;//data
@property (nonatomic, strong) MMNode *next;//next node
@end
单个link列表classMMList
#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
如何反转单个 link 列表?
- (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
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
中的 link
我写了一个 link 列表,但不知道如何反转,这是我的一些代码。
节点class MMNode
#import <Foundation/Foundation.h>
@interface MMNode : NSObject
@property (nonatomic, assign) int data;//data
@property (nonatomic, strong) MMNode *next;//next node
@end
单个link列表classMMList
#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
如何反转单个 link 列表?
- (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
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
中的 link