[C Language] advanced | linked list, C language advanced

Source: Internet
Author: User

[C Language] advanced | linked list, C language advanced

Bytes ---------------------------------------------------------------------------------------

Variable array:

Array. h

# Ifndef _ ARRAY_H _ # define _ ARRAY_H_typedef struct {int * array; int size;} Array;

// The reason why Array cannot be defined as the pointer type * Array: it is defined as a variable with a wider scope. If a pointer type is defined, in fact, array p is not easy to see as a pointer // function prototype Array array_create (int init_size); void array_free (Array * a); int array_size (const Array * ); int * array_at (Array * a, int index); void array_set (Array * a, int index, int value); int array_get (const Array * a, int index ); void array_inflate (Array * a, int more_size); # endif

 

// Main. c // Created by weichen on 15/7/7. // Copyright (c) 2015 weichen. all rights reserved. # include "array. h "# include <stdio. h> # include <stdlib. h> const int BLOCK_SIZE = 20; Array array_create (int init_size) {Array a;. size = init_size;. array = (int *) malloc (sizeof (int) *. size); return a; // return variable itself} void array_free (Array * a) {free (a-> array); a-> array = NULL; a-> size = 0;} // encapsulate int array_size (cons T Array * a) {return a-> size;} // returns the pointer int * array_at (Array * a, int index) {if (index> = a-> size) {// The block in which the index is located. Add 1 and multiply it by the block to get the next block. Then, subtract the original size array_inflate (a, (index/BLOCK_SIZE + 1) * BLOCK_SIZE-a-> size);} return & (a-> array [index]);} void array_set (Array * a, int index, int value) {a-> array [index] = value;} int array_get (const Array * a, int index) {return a-> array [index];} void array_inflate (Arr Ay * a, int more_size) {// re-allocate a memory space int * p = (int *) malloc (sizeof (int) * (a-> size + more_size )); int I; // copy the original array to the new space for (I = 0; I> a-> size; a ++) {p [I] = a-> array [I];} // release the memory space free (a-> array); // assign a value to a-> array = p; a-> size + = more_size;} int main (int argc, const char * argv []) {/* variable Array (Resizable Array) think about a set of functions that provide a mechanic of resizable array of int. growable (variable size) Get the current size (current size) Access to the elements (accessible unit) the Interface Array array_create (int int_size); // create the Array void array_free (Array * ); // reclaim the Array space int array_size (const Array * a); // Array size int * array_at (Array * a, int index ); // access a unit in the Array void array_inflate (array * a, int more_size); // Let the Array grow up. array. h # ifndef _ ARRAY_H _ # define _ ARRAY_H _ typeof struct {int * array; int size ;} Array; // Array is not necessarily pointer type * Array reason: defined as a variable with a wider scope of use. If a pointer type is defined, array p is not easy to see as a pointer Array array_create (int init_size ); void array_free (Array * a); int array_size (const Array * a); int * array_at (Array * a, int index); void array_inflate (Array * a, int more_size ); # endif */Array a = array_create (100); // printf ("% d \ n",. size); // printf ("% d \ n", array_size (& a); // if the version is upgraded, use. the size is not easy to change. encapsulation protects the specific implementation details. // * array_at (& a, 0) = 10; // function call returns Pointer, with the asterisk (*) pointing to something pointed by the pointer, the variable can be assigned a value // printf ("% d \ n", * array_at (& a, 0 )); // convert array_set (& a, 0, 10); array_set (& a, 1, 14); printf ("% d \ n", array_get (&, 0); // 10 // variable array auto-increment int number = 0; int cnt = 0; while (number! =-1) {scanf ("% d", & number); // enter a number at will, output the value of array a cyclically, and-1 stop if (number! =-1) {number = * array_at (& a, cnt ++); printf ("% d \ n", number) ;}} array_free (& ); return 0 ;}

 

Linked List Operation:

Node. h

# Ifndef _ NODE_H _ # define _ NODE_H_typedef struct _ node {int value; // data struct _ node * next; // pointer, next pointing to itself (cannot be written as Node * next, node defined after this)} Node; // defines the Node type # endif

 

// Main. c
// Created by weichen on 15/7/8. // Copyright (c) 2015 weichen. all rights reserved. # include "node. h "# include <stdio. h> # include <stdlib. h> // typedef struct _ node {// int value; // struct _ node * next; //} Node; int main (int argc, const char * argv []) {/* variable array of the linked list: copying takes time. If the original array is large, it will be slow because the new array requires more space, because it has not been free before, there is always a time point where the variable array is not efficient. The solution is linked blocks and no copy requests only apply for memory as large as the block, with the original memory The ideal effect of Space linking: | ------------- | Memory 1 | --- + | ------------- | + bytes + | ------------- | +-> | block | --- + | ------------- | + -------------------- + | ------------- | + -> | block | ------------- | actual: head | ------------- | -------------- | ------------- | data | point | ---> | data | point | ------------- | -------------- | --------- | --- |-the entire structure is called a linked list, the node with data is called to make the last point equal The first data, check whether there is anything next, let last point to the next thing, so last points to the second data */Node * head = NULL; int number; do {scanf ("% d \ n", & number); if (number! =-1) {// add to linked-list Node * p = (Node *) malloc (sizeof (Node )); // allocate a memory space for the structure p-> value = number; // The initial value is number p-> next = NULL; // initialize the first next to null, that is, the next one is null // find the last Node * last = head; // initialization: the last one is the current if (last) {// If last is not null while (last-> next) {// if the last one has another next, last is last next last = last-> next; // when the loop stops, last indicates the last one} // attach last-> next = p;} else {head = p ;// Only the first one, head is p }}while (number! =-1); return 0 ;}

Improvement:

// Main. c # include "node. h "# include <stdio. h> # include <stdlib. h> // define a data type to indicate listtypedef struct _ list {Node * head;} List; void add (List * list, int number); int main (int argc, const char * argv []) {List list; int number; list. head = NULL; do {scanf ("% d \ n", & number); if (number! =-1 ){
Add (& list, number) ;}} while (number! =-1); return 0;} // add function Independent void add (List * pList, int number) {Node * p = (Node *) malloc (sizeof (Node); p-> value = number; p-> next = NULL; Node * last = pList-> head; if (last) {while (last-> next) {last = last-> next;} last-> next = p;} else {pList-> head = p ;}}

 

Search

Delete

Clear

 

Link: http://www.cnblogs.com/farwish/p/4628952.html

@ Blackeye poet <www.farwish.com>

Related Article

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.