//
Viewcontroller.m
Linked list
//
Created by Zhang Kaize on 16/1/26.
COPYRIGHT©2016 year Rytong_zkz. All rights reserved.
//
#import "ViewController.h"
/*
The function of the static keyword:
(1) The function body static variable scope is the function body, different from the auto variable, the memory of the variable is only assigned once,
Therefore, its value remains the last value at the next call;
(2) The static global variable within the module can be accessed by functions used within the module, but not by other functions outside the module;
(3) The static function within the module can only be called by other functions within this module, and the use of this function is limited to the declaration
within its module;
(4) A static member variable in a class is owned by the entire class and has only one copy of all objects of the class;
(5) The static member function in a class is owned by the entire class, which does not receive the this pointer, and therefore can only access static member variables of the class.
*/
typedef struct list{
int num;
struct list * NEXT;
}list;
@interface Viewcontroller ()
@end
@implementation Viewcontroller
-(void) Viewdidload {
[Super Viewdidload];
int a =[self Lianbiao];
printf (@ "______________%d", a);
}
-(void) Stransition: (int**) p
{
printf ("p =%p\n", p);
}
-(int) Lianbiao
{
List * head = NULL;
int i = 0;
Head = Initlist ();
printf ("head =%p", head);
if (head = = NULL) {
return-1;
}
Insertlist (&head, 2);
Insertlist (&head, 4);
Insertlist (&head, 6);
Insertlist (&head, 3);
Insertlist (&head, 1);
Prilist (head);
DeleteList (&head, 1);
Prilist (head);
DeleteList (&head, 2);
Prilist (head);
DeleteList (&head, 3);
Prilist (head);
FreeList (&head);
return 0;
}
Initialize the list of links
Static List * Initlist ()
{
List * list = NULL;
List = (list*) malloc (sizeof (list));
if (list = = NULL) {
return NULL;
}
List->next = NULL;
return list;
}
Print linked list information
static void Prilist (list* List)
{
if (list = = NULL) {
Return
printf ("------------\ n");
}
while (list->next) {
printf ("%d\n", List-and next->num);
List = list, Next;
}
printf ("-----------\ n");
Return
}
Insert a linked list node based on num size
static int insertlist (list** List, int num)
{
List * now = NULL;
List * head = *list;//Here the address of *list is just beginning to initialize the address that gets the address head
now = Initlist ();
if (now = = NULL) {
return-1;
}
Now, num = num;
while (head->next && num >= head, Next, num) {
Head = Head->next;
}
if (head-to-next = NULL) {
Head--next = now;
}else{
Now-next = head-next;
Head--next = now;
}
return 0;
}
Calculate the length of a linked list
static int Numlist (List * head)
{
int len = 0;
while (head && head, next) {
Len + +;
Head = head Next;
}
return Len;
}
Call free to release the node of the linked list individually
static void FreeList (list** List)
{
list* head = *list;
list* p = NULL;
while (head) {
p = head;
Head = Head->next;
Free (p);
}
printf ("Free List OK \ n");
Return
}
Delete the node at the corresponding location
static int DeleteList (list** List, int location)
{
List * head = *list;
list* p = NULL;
int i = 1;
if (location <= 0 | |, location > numlist (head)) {
return-1;
}
while (i++ <location && Head, next) {
Head = head Next;
}
p = Head, next;
Head-next = P-Next;
if (p) {
Free (p);
p = NULL;
}
return 0;
}
@end
Simple and practical C-language chain list