类,结构体,指针,引用

1.类和结构体

类的定义:

#include<bits/stdc++.h>
using namespace std;
class Person
{
	private:
		int age=11,height;
		double money;
		string books[100];
	public:
		string name;
		void say()
		{
			cout<<"I'm"<<name<<endl;
		}
		int get_age()
		{
			return age;
		}
		void set_age(int age1)
		{
			if(age1>=0&&age1<=130)
			{
				age=age1;
			}
		}
		void add_money(double x)
		{
			money+=x;
		}
};
int main()
{
	Person a;
	a.set_age(12);
	cout<<a.get_age();
	return 0;
}

类中的变量和函数被统一称为成员变量。
private后面的内容是私有成员变量,在类的外部不能访问;public后面的内容是公有成员变量,在类的外部可以访问。
类的使用:

#include<bits/stdc++.h>
using namespace std;
class Person
{
	private:
		int age=11,height;
		double money=0;
		string books[100];
	public:
		string name;
		void say()
		{
			cout<<"I'm"<<name<<endl;
		}
		int get_age()
		{
			return age;
		}
		void set_age(int age1)
		{
			if(age1>=0&&age1<=130)
			{
				age=age1;
			}
		}
		void add_money(double x)
		{
			money+=x;
		}
};
int main()
{
	Person a;
	a.name="123";	//正确:访问公有变量
	a.age=12;	    //错误:访问私有变量
	a.set_age(12);  //正确:set_ade是公有成员变量
	a.add_money(100);
	a.say();
	cout<<a.get_age();
	return 0; 
}

结构体和类的作用是一样的。不同点在于类默认是private,结构体默认是public。

#include<bits/stdc++.h>
using namespace std;
struct Person
{
	private:
		int age=11,height;
		double money=0;
		string books[100];
	public:
		string name;
		void say()
		{
			cout<<"I'm"<<name<<endl;
		}
		int get_age()
		{
			return age;
		}
		void set_age(int age1)
		{
			if(age1>=0&&age1<=130)
			{
				age=age1;
			}
		}
		void add_money(double x)
		{
			money+=x;
		}
}Person_a,Person_b,Persons[100];

2.指针和引用

指针:储存变量,地址的变量:

    int a=10;
	int *p=&a;
	cout<<p;//输出的是地址
	cout<<*p+10;//输出的是值

数组名是一种特殊的指针。指针可以做运算:

    int a[5]{1,2,3,4,5};
	for(int i=0;i<5;i++)
	cout<<*(a+i)<<endl;

引用和指针类似,相当于给变量起了个别名

    int a=10;
	int &p=a;
	p-=10;
	cout<<a;

3.链表

#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int val;
	Node *next;
}*head;
int main()
{
	Node *x1=new Node();
	x1->val=1;
	Node *x2=new Node();
	x2->val=2;
	x1->next=x2;
	Node *x3=new Node();
	x3->val=3;
	x2->next=x3;
	while(x1)
	{
		cout<<x1->val<<endl;
		x1=x1->next;
	}
	return 0;
}
#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int val;
	Node *next;
}*head;
int main()
{
	for(int i=1;i<=5;i++)
	{
		Node *p=new Node;
		p->val=i;
		p->next=head;
		head=p; 
	}
	for(Node *p=head;p;p=p->next)
	cout<<p->val<<endl;
}

删除

#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int val;
	Node *next;
}*head;
void Delete(int n)
{
	Node *temp1=head;
	if(n==1)
	{
		head=temp1->next;
		delete temp1;
		return;
	}
		for(int i=1;i<n-1;i++)
		temp1=temp1->next;
		Node *temp2=temp1->next;
		temp1->next=temp2->next;
		delete temp2;
}
int main()
{
	for(int i=5;i>0;i--)
	{
		Node *p=new Node;
		p->val=i;
		p->next=head;
		head=p; 
	}
	Delete(4);
	for(Node *p=head;p;p=p->next)
	cout<<p->val;
	return 0;
} 

插入

#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int val;
	Node *next;
}*head;
void Insert(int n,int s)
{
	Node *temp1=new Node();
	temp1->val=s;
	if(n==1)
	{
		temp1->next=head;
		head=temp1;
		return;
	}
	Node *temp2=head;
	for(int i=0;i<n-2;i++)
	temp2=temp2->next;
	temp1->next=temp2->next;
	temp2->next=temp1;
}
int main()
{
	for(int i=5;i>0;i--)
	{
		Node *p=new Node;
		p->val=i;
		p->next=head;
		head=p; 
	}
	Insert(4,0);
	for(Node *p=head;p;p=p->next)
	cout<<p->val;
	return 0;
} 

反转

#include<bits/stdc++.h>
using namespace std;
struct Node
{
	int val;
	Node *next;
}*head;
void fz()
{
	Node *prev=NULL;
	Node *curr=head;
	Node *next;
	while(curr!=NULL)
	{
		next=curr->next;
		curr->next=prev;
		prev=curr;
		curr=next;
	}
	head=prev;
}
i
{
	for(int i=1;i<=3;i++)
	{              
		Node *p=new Node();
		p->val=i;
		p->next=h
		head=p;      
		ead;
	f
	for(N
	cout<<p->val<<" ";           
	return 0;         
	z();

双向链表

#include<bits/stdc++.h>
using namespace std;
struct Double_Node
{
	int val;
	Double_Node *next;
	Double_Node *prev;
}*head;
int main()
{
	for(int i=1;i<=10;i++)
	{
		Double_Node *p=new Double_Node();
		p->val=i;
		p->next=head;
		head=p;
		if(i!=1)
		{
			head->next->prev=p;
		}
	}
	for(Double_Node *p=head;p;p=p->next)
	cout<<p->val<<" ";
	return 0;
}

双向链表删除

#include<bits/stdc++.h>
using namespace std;
struct Double_Node
{
	int val;
	Double_Node *next;
	Double_Node *prev;
}*head;
void Delete(int n)
{
	Double_Node *temp1=head;
	if(n==1)
	{
		head=temp1->next;
		head->prev=NULL;
		delete temp1;
		return ; 
	}
	for(int i=1;i<n-1;i++)
	temp1=temp1->next;
	Double_Node *temp2=temp1->next;
	if(temp2->next)
	{
		temp1->next=temp2->next;
		temp1->next->prev=temp1;
	}
	else
	temp1->next=NULL;
	delete temp2;
}
int main()
{
	for(int i=1;i<=10;i++)
	{
		Double_Node *p=new Double_Node();
		p->val=i;
		p->next=head;
		head=p;
		if(i!=1)
		{
			head->next->prev=p;
		}
	}
	Delete(5);
	for(Double_Node *p=head;p;p=p->next)
	cout<<p->val<<" ";
	return 0;
}

双向链表插入

#include<bits/stdc++.h>
using namespace std;
struct Double_Node
{
	int val;
	Double_Node *next;
	Double_Node *prev;
}*head;
void Insert(int n,int i)
{
	Double_Node *temp1=new Double_Node();
	temp1->val=i;
	if(n==1)
	{
		temp1->next=head;
		head->prev=temp1;
		head=temp1;
		return ;
	}
	Double_Node *temp2=head;
	for(int i=1;i<n;i++)
	temp2=temp2->next;
	if(temp2->next)
	{
	temp1->next=temp2->next;
	temp1->next->prev=temp1;
	}
	else
	temp1->next=NULL;
	temp1->prev=temp2;
	temp2->next=temp1;
}
int main()
{
	for(int i=1;i<=10;i++)
	{
		Double_Node *p=new Double_Node();
		p->val=i;
		p->next=head;
		head=p;
		if(i!=1)
		head->next->prev=p;
	}
	Insert(5,3);
	for(Double_Node *p=head;p;p=p->next)
	cout<<p->val<<" ";
	return 0;
}
posted @ 2024-12-06 19:26  andy_qwertyuiop  阅读(28)  评论(0)    收藏  举报