Objective-c algorithm (1. linked list) and objective-c Algorithm

Source: Internet
Author: User

Objective-c algorithm (1. linked list) and objective-c Algorithm
Introduction to linked list:

A linked list is the most basic data structure. It is of great help when maintaining the set data, especially in addition and deletion. Today, we will summarize the linked list we have learned.
In ios development, mastering some common algorithms can help us develop more efficiently.
The total category of the linked list:

  • Single-chain table
  • Two-way linked list
  • Cyclic linked list

Let's talk about the order linked list today: elements are linked by a single pointer. The linked list of this structure allows you to traverse from the first element to the last element.

Each element is connected by a pointer. Each element contains two parts: a data member and a pointer called next. With this structure, the next pointer of each element is set to the element following it. The Next of the last element is set to NULL, which simply represents the tail end of the linked list. The element at the beginning of the linked list is "head", and the element at the end of the linked list is called "tail".

Figure 1

According to the above, we can regard the linked list as a series of continuous elements. We need to know that these elements are dynamically allocated in the memory. In fact, the connection between elements is to ensure that we can access each element. If one of the links is lost, all links starting from the link will be lost.

I once read a sentence in a book: "How weak are you and how strong your strength is?" This sentence is a perfect description of linked lists.

Enter today's topic:

Single-chain table Interface Definition: Initialize linked list:
-(void)listInit :(List*) list;

This function must be called before other operations ., Use listInit to initialize a linked list so that we can perform other operations. Initialization is very simple. You only need to set the size to 0 and the head and tail values to null to complete the initialization.

After the specified element, add:
-(int)listInsNext:(List*) list over:(ListElemt *) element over:(ListElemt *)newElement;

Simply put, the insert operation is to establish A new connection to the linked list. For example, if there are two nodes A and C, add Node B after node. First, let B's next pointer point to C. Then C, and the next pointer of A points to B. This completes the simple operation. But when it is inserted to the head position, there will be no nodes in front of the new element. What should we do? Simply put, you just need to point the next pointer of the new element to the head of the current linked list. When the input element is empty, insert it directly to the header.

Figure 1:

Remove the element after the specified element:
-(int)listRemNext:(List*) list over:(ListElemt *) element;

Remove the elements behind the element instead of the element itself. We will explain the reason later. You need to consider two points to remove the header node and other nodes.
The operation is actually very simple. For example, if there are three nodes A, B, and C, we only need to apply for A D, let D point to the next element of A, and then A next points to the next Of. In this way, we will remove B directly, leaving A and C. After that, ARC will automatically recycle B.

Figure 2:

Output linked list
-(void)outPut:(List*) list;

Uses a loop to initialize a pointer pre so that the Pointer Points to the list head. Judge after each loop and re-plan: pre = pre-> next

The following are macro definitions for quick access and monitoring of List and listelemts:
#define list_size(list)((list)->size)#define list_head(list)((list)->head)#define list_tail(list)((list)->tail)#define list_is_head(list,element)((element)==(list)->head ? 1:0)#define list_is_tail(element)((element)->next = NULL ? 1:0)#define list_data(element)((element)->data)#define list_next(element)((element)->data)

Listelemts. h code:

//// Listelemts. h // OK /// Created by MrLoong on 15/8/8. // Copyright (c) 2015 MrLoong. all rights reserved. // # import <Foundation/Foundation. h> @ interface listelemts: NSObject {@ public id data; @ public listelemts * next;} @ end

The list. h code is as follows:

// List. h // Created by MrLoong on 15/8/8. // Copyright (c) 2015 MrLoong. all rights reserved. // # import <Foundation/Foundation. h> # import "listelemts. h "@ interface List: NSObject {@ public listelemts * head; @ public listelemts * tail; @ public int size;} // initialize the linked List-(void) listInit :( List *) list; // Add newElement-(int) listInsNext :( List *) list over :( listelemts *) element over :( listelemts *) newElement after the specified element; // remove the specified Element-(int) listRemNext :( List *) list over :( listelemts *) element; // outPut linked List-(void) outPut :( list *) List; # define list_size (list)-> size) # define list_head (list)-> head) # define list_tail (list) -> tail) # define list_is_head (list, element) (element) = (list)-> head? 1:0) # define list_is_tail (element)-> next = NULL? 1:0) # define list_data (element)-> data) # define list_next (element)-> data) @ end

The list. m code is as follows:

//// List. m // OK /// Created by MrLoong on 15/8/8. // Copyright (c) 2015 MrLoong. all rights reserved. // # import "List. h "@ implementation List // initialize the linked List-(void) listInit :( list *) list {list-> size = 0; List-> head = NULL; list-> tail = NULL; return;} // insert newElement-(int) listInsNext :( List *) list over :( listelemts *) element over :( listelemts *) after element *) newElement {if (newElement = NULL) {return-1;} if (element = N ULL) {if (list_size (list) = 0) {list-> tail = newElement;} newElement-> next = list-> head; list-> head = newElement ;} else {if (element = NULL) {list-> tail = newElement;} newElement-> next = element-> next; element-> next = newElement ;} list-> size ++; return 0;} // remove the element after the specified element-(int) listRemNext :( List *) list over :( listelemts *) element {listelemim * oldEmement; if (list_size (list) = 0) {return-1;} if (elemen T = NULL) {oldEmement = list-> head; list-> head = list-> head-> next; if (list_size (list) = 0) {list-> tail = NULL;} else {if (element-> next = NULL) {return-1;} oldEmement = element-> next; element-> next = element-> next; if (element-> next = NULL) {list-> tail = element ;}} // free (_ bridge void *) (oldEmement); return 0 ;}// outPut linked List-(void) outPut :( List *) list {listelemts * pre = list-> head; while (pre! = Nil) {NSLog (@ "% @", pre-> data); pre = pre-> next; }}@ end

Main. m code:

//// Main. m // OK /// Created by MrLoong on 15/8/8. // Copyright (c) 2015 MrLoong. all rights reserved. // # import <Foundation/Foundation. h> # import "List. h "# import" listelemts. h "int main (int argc, const char * argv []) {@ autoreleasepool {// create element List * list = [[List alloc] init]; [list listInit: list]; ListElemt * element1 = [[listelemalloc] init]; element1-> data = [NSNumber numberWithInt: 1]; listelemim * element2 = [[listelemim alloc] init]; element2-> data = [NSNumber numberWithInt: 2]; ListElemt * element3 = [[ListElemt alloc] init]; element3-> data = [NSNumber numberWithInt: 3]; listelemim * element4 = [[listelemr alloc] init]; element4-> data = [NSNumber numberWithInt: 4]; listelemr * element5 = [[listelemr alloc] init]; element5-> data = [NSNumber numberWithInt: 5]; ListElemt * element6 = [[ListElemt alloc] init]; element6-> data = [NSNumber numberWithInt: 6]; // create the element [list listInsNext: list over: list-> head over: element1]; [list listInsNext: list over: element1 over: element2]; [list listInsNext: list over: element2 over: element3]; [list listInsNext: list over: element3 over: element4]; [list listInsNext: list over: element4 over: element5]; [list listInsNext: list over: element5 over: element6]; [list outPut: list]; // Delete element3 [list listRemNext: list over: element2], an element after element2; printf ("========================================== =======\ n "); printf ("inserted \ n "); printf ("========================================== =======\ n "); [list outPut: list]; printf ("========================================== =======\ n "); printf ("deleted \ n "); printf ("========================================== =======\ n ");} return 0 ;}

Copyright Disclaimer: This article is an original article by the blogger and cannot be reproduced without the permission of the blogger.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.