今天学习了双链表:
#include<iostream>
using namespace std;
struct Node
{
int data;
Node* pre;
Node* next;
};
class double_LinkList
{
public:
double_LinkList();
~double_LinkList();
void Insert(int pos, int val);
int Get();
void Print();
private:
Node* head;
int length;
};
double_LinkList::double_LinkList()
{
head = new Node;
head->next = NULL;
head->pre = NULL;
length = 0;
}
double_LinkList::~double_LinkList()
{
Node* q = NULL;
Node* q_pre = NULL;
for (Node* p = head->next; p != NULL; p = q)
{
q = p->next;
delete p;
}
for (Node* p_pre = head->pre; p_pre != NULL; p_pre = q_pre)
{
q_pre = p_pre->next;
delete p_pre;
}
}
void double_LinkList::Insert(int pos, int val)
{
if (pos < 1)throw"位置错误";
int count = 0;
Node* s = new Node;
s->data = val;
if (head->next == NULL)//只有头结点
{
s->next = NULL;
s->pre = head;
head->next = s;
count++;
}
else
{
s->pre = head;
s->next = head->next;
head->next->pre = s;
head->next = s;
count++;
}
length++;
}
int double_LinkList::Get()
{
return length;
}
void double_LinkList::Print()
{
for (Node* p = head->next; p != NULL; p = p->next)
{
cout << p->data << "->";
}
cout << "None" << endl;
}
int main()
{
double_LinkList list;
int n, pos, val;
char command;
while (cin >> command)
{
if (command == 'E')
break;
try {
switch (command)
{
case'I':
cin >> n;
for (int i = 0; i < n; i++)
{
cin >> pos >> val;
list.Insert(pos, val);
}
break;
case 'G':
cout << list.Get();
break;
case 'V':
list.Print();
break;
}
}
catch (const char* str)
{
cout << str;
}
}
return 0;
}
栈:
#include<iostream>
using namespace std;
class Point
{
int data;
public:
int getdata();
bool setdata(int data);
};
int Point::getdata()
{
return this->data;
}
bool Point::setdata(int data)
{
this->data = data;
return 1;
}
class Stack {
public:
Stack();
int get_top();//得到最上面的元素
bool push(int data);//入栈
bool pop();//出栈
bool emp();//得知栈空不空
int Get_length();//栈的长度
private:
Point* top;
Point base[100];
int length;
};
Stack::Stack() {
top = base;
length = 0;
}
int Stack::get_top()
{
if (length == 0)
{
return 0;
}
else
{
return (top - 1)->getdata();
}
}
bool Stack::push(int data)
{
if (length == 100)
{
return 0;
}
top->setdata(data);
top++;
length++;
return 1;
}
bool Stack::pop()
{
if (length == 0)
{
return 0;
}
top--;
length--;
return 1;
}
bool Stack::emp() {
if (top == base)
{
return 1;
}
else {
return 0;
}
}
int Stack::Get_length(){
return length;
}
int main()
{
Stack s = Stack();
s.push(1);
s.push(2);
s.push(3);
s.push(4);
for (int i = 0; i < 5; i++) {
cout << s.get_top() << "\t";
s.pop();
}
}
java的方法:
package methoddemo1;
//求数组最大值
public class methoddemo5 {
public static int get_max(int[] arr) {
int max = arr[0];
for (int i = 1; i < arr.length; i++)
{
if(arr[i]>max)
{
max = arr[i];
}
}
return max;
}
public static void main(String[] args) {
int[] age = {1,2,3,4,6,3};
System.out.println(get_max(age));
}
}
/*
方法调用前存放在方法区
调用时存放在栈
*/
方法的重载:
package overload;
public class methoddemo1 {
public static void main(String[] args) {
fire();
fire("岛国");
fire("岛国",2);
}
public static void fire()
{
System.out.println("默认发射一枚武器给米国");//还可以写成fire("米国")
}
public static void fire(String location)
{
System.out.println("默认发射一枚武器给"+location);//还可以写成fire(location, 1);
}
public static void fire(String location, int num)
{
System.out.println("默认发射"+num+"枚武器给"+location);
}
}
java引用类型的参数的传递机制:
package param;
//引用类型的参数传递机制(值传递)
public class methoddemo2 {
public static void main(String[] args) {
int[] arrs = {10,20,30};
change(arrs);
System.out.println(arrs[1]);//输出后这里会变成222因为java是值传递这里传递的是地址这里地址不改变但是值变了
}
public static void change(int[] arrs)
{
System.out.println(arrs[1]);
arrs[1] = 222;
System.out.println(arrs[1]);
}
}
关键字return的用法:
package Return;
//关键字return的作用
public class returndemo {
public static void main(String[] args) {
System.out.println("main开始");
chu(10, 0);
System.out.println("main结束");
}
public static void chu(int a, int b){
if(b == 0)
{
System.out.println("除数不能为0!");
return;//立即跳出当前方法的执行并结束当前方法,不会结束main方法
}
int c = a / b;
System.out.println("结果是:"+c);
}
}
以及做了一下上机实验的作业但只做了一半明天早起接着做吧。
总结一下今天发现自己有点急功近利了强行学一堆跨度大的东西效率太低明天适当降低一下节奏吧。