l1--Data Structure Introduction and preliminary knowledge

Source: Internet
Author: User

Introduced

How to store large and complex problems in reality in a specific data type and a specific storage structure into the main memory (memory), and on this basis to implement a function (such as finding an element, delete an element, The corresponding operation to sort all the elements is called an algorithm.

Data structure = individual + individual relationship

Algorithm = manipulation of stored data (methods and procedures for solving problems)

Criteria for measuring algorithms
    • 1. Time complexity: The approximate number of times the program will be executed, rather than the time of execution;
    • 2. Spatial complexity: The maximum memory occupied by the algorithm during execution;
    • 3. Degree of difficulty
    • 4. Robustness
The status of data structures

The data structure is the core content of the software.

Program = data storage + data manipulation + languages that can be computed for storage (c,c++, etc.)

Preliminary Knowledge-pointers

The importance of pointers: pointers are the souls of C language

Address: Memory unit number, non-negative integer starting from 0, range: 0---ffffffff[0---4g-1]

Pointer: The pointer is the address address is the pointer

Pointer variable: Is the variable that holds the address

#include <stdio.h>intMainvoid){    int* p;//p is a pointer variable, and an int * indicates that the P variable can store only the address of the INT type variable    /*Ps:int * is a whole (define a pointer variable, like int), do not put *p when a whole int p equals to define a p variable int * p equivalent to define a p-pointer variable*/    inti =Ten; intJ; P= &i; J= *p;//equivalent to j = i//the p at this time holds the address I of I, at this time the * play the role of indirect value , *p is equal. printf"i =%d, j =%d, *p =%d\n", I, J, *p); return 0;}/*output Result: I = ten, j = ten, *p = ten*/

Pointers and functions:

#include <stdio.h>voidFint* p)//The same last program: instead of defining a formal parameter named *p, it defines a pointer variable p that is an int * pointer variable type{    *p = -;//the p at this time holds the address I of I, at this time the * play the role of the indirect value (* can only be used in the pointer variable), *P is equal. }intMainvoid){    inti =9; F (&i); printf ("i =%d\n", i); return 0;}/*output Result: i = 100
PS: You can use the above example to make variables between different functions , imagine if there is no pointer variable, you can also make a variable between different functions to use*/
Pointers and Arrays: array names: one-dimensional array namesis a Pointer Constants; it holds a one-dimensional array the address of the first element; Its value cannot be changed; one-dimensional array name point to theis the first element of the array. The relationship between subscript and pointer: A[i] (<==>) * (A + i)
#include <stdio.h>intMainvoid){    inta[5] = {1,2,3,4,5}; printf ("%p\n", A +1); printf ("%p\n", A +2); printf ("%p\n", A +3); printf ("----------------------------\ n"); printf ("%d\n", *a +3);//*a + 3 equivalent to a[0] + 3    return 0;}/*output Result: 0028fef00028fef40028fef8----------------------------4*/

How to modify the contents of an array in the keynote function through the function

1. A pointer variable that holds the first element of the array; 2. An shaping variable that holds the length of an array element.
#include <stdio.h>voidShowarray (int* p,intLen) {    inti; p[0] = -1;//p[0] = = *p;p[i] is the a[i of the main function]     for(i =0; i < Len; ++i) {printf ("%d\n", P[i]); } printf ("-----------------------\ n" );}intMainvoid){    inta[5] = {1,2,3,4,5}; Showarray (A,5);//A is equivalent to &a[0],printf ("%d\n", a[0]); return 0;}/*results: -12345------------------------1*/
Preparatory knowledge-structure

1. Why the structure is present

In order to represent some complex data, the common basic type variable does not meet the requirements.

2. What is a structural body?

A struct is a composite data type that users define themselves according to their actual needs.

3. How to use the structure body

#include <stdio.h>#include<string.h>structstudent{intSID; Charname[ $]; intAge ;};//semicolons cannot saveintMainvoid){    structStudent st = { +,"Zhangsan", -}; printf ("%d%s%d\n", St.sid, St.name, st.age); St.sid= About; strcpy (St.name,"Lisi"); St.age= A; printf ("%d%s%d\n", St.sid, St.name, st.age); structStudent *PST; PST= &St; PST->sid = the;//Pst->sid is equivalent to (*PST). Sid, and (*PST). SID is equivalent to St.sidprintf"%d%s%d\n", St.sid, St.name, st.age); return 0;}/*output: Zhangsan 2099 Lisi 2288 Lisi*/ 

4. Precautions:

    • Structural variables cannot be subtraction, but can be assigned to each other;

Common structure variables and pointer variables of structural bodies

#include <stdio.h>#include<string.h>structstudent{intSID; Charname[ $]; intAge ;};//semicolons cannot savevoidFstructStudent *);voidGstructStudent);voidG2 (structStudent *);intMainvoid){    structStudent St; // to allocate good memory F for St  (&St); //g (ST);//direct the global variable (at least 208 bytes) When the argument passes, consumes memory, consumes timeG2 (&AMP;ST);//only 4 bytes//printf ("%d%s%d\n", St.sid, St.name, st.age);    return 0;}voidFstructStudent *PST) {    (*PST). Sid = About; strcpy (PST->name,"Zhangsan"); PST->age = A;}voidGstructStudent St) {printf ("%d%s%d\n", St.sid, St.name, st.age);}voidG2 (structStudent *PST) {printf ("%d%s%d\n", Pst->sid, Pst->name, pst->Age );}/*output Result: Zhangsan*/
Preliminary knowledge-Dynamic memory
#include <stdio.h>#include<malloc.h>intMainvoid){    inta[5] = {1,2,3,4,5};//unable to dynamically control number of leader degrees    inti; intLen; printf ("Please input length:len ="); scanf ("%d", &Len); int* PARR = (int*) malloc (sizeof(int) *Len);  for(i =0; i < Len; ++i) {scanf ("%d/n", &Parr[i]); } printf ("--------------------------\ n");  for(i =0; i < Len; ++i) {printf ("%d\n", * (PARR +i));    } free (PARR); return 0;}/*output: Please input length:len = 323--------------------------234455*/

l1--Data Structure Introduction and preliminary knowledge

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.