Structured learning notes

Source: Internet
Author: User

The following are some of the mistakes made when you are a beginner's struct.

 

First come to some miscellaneous

struct f{    string name;};struct students{    int num;    string name;    students *next;    students friends;//    f fri;};void main(){    students boy[40];    students polyy,*li = &polyy;    polyy.num;    polyy.name;    polyy.friends.name;    polyy.next->num;    li->num;    li->name;    li->friends.name;    li->next->num;}
Basic knowledge

 

 

Error 1: The structure body uses itself to define entities

1 struct student2 {3     int num;4     string name;5     student friends;6 };

Because there is no memory address space before the struct is executed. The fifth line defines an object, which is obviously wrong and should be in the form of a pointer.

1 struct students2 {3     int num;4     string name;5     students *friends;6 };

 

Error 2: errors in struct Declaration

// The correct representation. The struct is defined before it is used. struct student {int num ;}; int main () {struct student poly; return 0 ;}
// Error struct student; int main () {struct student poly; // wrong return 0;} struct student {int num ;};

The error is that, before running a struct to generate an object, the object is defined and there is no space (poor expression... You can define it as a pointer in the form of rows 5th. Unlike a function, a function is called only and no "extra" address space is generated.

// Correct struct student; int main () {struct student * poly; return 0;} struct student {int num ;};

 

Linked List Learning

The basic operations of a linked list include creating a linked list, inserting nodes, deleting nodes, and accessing nodes. The form is as follows:

struct linkRec{    int data;    linkRec *next;};

  Header Node: A special node in the linked list. It does not store data, but to ensure that each element has a precursor. It is often used in a single-chain table, especially when a single-chain table is inserted before the first node.

  Insert a linked list Node:.

  Delete A linked list NodeThe opposite is insert p-> next = p-> next, but pay attention to reclaim space to prevent memory leakage.

// Create and access a single-chain table struct linkrec {int data; linkrec * Next;}; int main () {int X; linkrec * head, * P, * rear; head = rear = new linkrec; while (true) {CIN> X; If (x = 0) break; P = new linkrec; P-> DATA = X; // P = rear-> next; // error rear-> next = P; // link P to the end of the table. Rear = P;} rear-> next = NULL; cout <"connects of the linkrec is: \ n"; P = head-> next; while (P! = NULL) {cout <p-> data <"\ t"; P = p-> next;} return 0 ;}
// Joseph Ring problem struct node {int data; node * Next;}; int main () {int N; node * P, * q, * head; P = head = new node; cout <"Please input N: \ t"; CIN> N; P-> DATA = 0; For (INT I = 1; I <n; I ++) {q = new node; q-> DATA = I; P-> next = Q; P = Q;} p-> next = head; Q = head; while (Q-> next! = Q) {P = Q-> next; q = p-> next; P-> next = Q-> next; Delete Q; q = p-> next ;} cout <q-> data <Endl; return 0 ;}
struct studant1{    string sex;};struct studant{    int num;    string name;    studant1 se;};int main(){    studant poly,*p = &poly;    cout<<"poly`s num name sex: \n";    cin>>p->num>>p->name;    cout<<poly.num<<"\t"<<poly.name<<endl;    cin>>p->se.sex;    cout<<poly.se.sex<<endl;//p->se->sex<<endl;        return 0;}
Basic
struct pointT{    double x,y;};pointT setpoint(double x,double y);pointT getpoint(double);int main(){    pointT p1;    p1 = setpoint(1,2);    cout<<p1.x<<"\t"<<p1.y<<endl;    return 0;}pointT setpoint(double x,double y){    pointT p;    p.x = x;    p.y = y;    return p;}
Struct as a function parameter

 

I am very sorry for all kinds of misexpressions.

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.