今天在写平衡二叉树的时候发现自己对于链表还是不够理解,建立树指针和后面的节点变量之间的链接和交互搞得很乱,给同学讲解指针变量和普通变量的时候也没有讲的很清楚,所以找到之前写的链表的代码回顾一遍,希望能够有所启发
头文件 students_1.h
#pragma once
#include<iostream>
#include<string.h>
using namespace std;
typedef struct //学生结构
{
int no;
string name;
int grand;
}students;
struct Lnode //学生链表
{
students data;
struct Lnode* next;
};
typedef struct Lnode lnode;
typedef struct Lnode* linklist;
bool initlist_L(linklist& l) //初始化链表
{
l = new lnode;
l->next = NULL;
return true;
}
bool getdata_L(linklist& l, int i, students& e) //取出链表里面索引为i的学生的信息到e
{
if (i < 1)
return false;
linklist p = l->next;
int j = 1;
while (p && j < i)
{
p = p->next;
j++;
}
if (p == NULL)
return false;
e = p->data;
return true;
}
bool finddata_L(linklist& l, int no, students& e) //找到链表里学号为no的学生并把学生信息返回e
{
linklist p = l->next;
while (p)
{
if (p->data.no == no)
{
e = p->data;
return true;
}
p = p->next;
}
cout << "Can not get the students !" << endl;
return false;
}
bool finddata_L(linklist& l, string a, students& e) //取出链表里姓名为a的学生的信息返回e
{
linklist p = l->next;
while (p)
{
if (p->data.name == a)
{
e = p->data;
return true;
}
p = p->next;
}
cout << "Can not get the students !" << endl;
return false;
}
bool deledata_L(linklist& l, int a) //删除所应为a的节点
{
if (a < 1)
return false;
linklist p = l->next;
int j = 1;
while ((p->next) && j < a)
{
p = p->next;
j++;
}
if (!(p->next) || j > a)
{
cout << "The num of students is wrong ,please enter again!" << endl;
return false;
}
linklist q = p->next;
p->next = q->next;
delete q;
return true;
}
bool listinsert_L(linklist& l, int i, students e) //在索引为i的位置插入学生e的节点
{
if (i < 1)
return false;
linklist p = l->next;
int j = 0;
while (p && j < i - 1)
{
p = p->next;
++j;
}
lnode* s1 = new lnode;
s1->data = e;
s1->next = p->next;
p->next = s1;
return true;
}
bool print_L(linklist l) //输出链表
{
linklist q = l->next;
if (!q)
{
cout << "The SQlist is empty !" << endl;
return false;
}
while (q)
{
cout << "Sno = " << q->data.no << ", Sname is " << q->data.name << ", Sgrand = " << q->data.grand << endl;
q = q->next;
}
return true;
}
bool guibing_L(linklist p, linklist q, linklist& l) //归并链表pq到l
{
linklist q1 = q->next;
linklist p1 = p->next;
linklist r;
l = q;
l->next = NULL;
delete q;
r = l;
while (p1 != NULL && q1 != NULL)
{
if (p1->data.grand >= q1->data.grand)
{
r->next = p1;
p1 = p1->next;
r = r->next;
}
else
{
r->next = q1;
q1 = q1->next;
r = r->next;
}
}
if (p1 != NULL)
r->next = p1;
if (q1 != NULL)
r->next = q1;
return true;
}
int bijiao_no(students s1, students s2) //通过学号比较学生s1 s2 的大小
{
if (s1.no > s2.no)
{
return s1.no;
}
else
return s2.no;
}
int bijiao_grand(students s1, students s2) //比较s1 s2两个人的成绩
{
if (s1.grand > s2.grand)
{
return s1.grand;
}
else
return s2.grand;
}
void swap_S(students& s1, students& s2) //交换S1S2通过引用
{
students s3 = s1;
s1 = s2;
s2 = s3;
}
void swap_s(students* s1, students* s2) //交还S1S2通过指针
{
students s3 = *s1;
*s1 = *s2;
*s2 = s3;
}
bool print_S(students s) //输出学生信息
{
cout << "Sno = " << s.no << ", Sname is " << s.name << ", Sgrand = " << s.grand << endl;
return true;
}
主函数
#include <iostream>
#include"students_1.h"
#include<string.h>
/*
* initlist_L 初始化链表
* getdata_L 获取 l链表 第n个节点上的students信息
* finddata_L 获取l链表上某学号姓名的学生信息
* deledata_L 删除l链表上第n个节点
* listinsert_L 向链表l第n个节点位置插入学生元素
* print_L 遍历并输出链表L
*/
int main()
{
linklist l;
initlist_L(l);
linklist q;
initlist_L(q);
linklist p;
initlist_L(p);
students s1 = { 001, "zq", 93 };
students s2 = { 002, "lmk", 91 };
students s3 = { 003, "cyg", 92 };
students s4 = { 004, "lmk", 95 };
students s5 = { 005, "cyg", 93 };
lnode nod1 = { {s1} };
lnode nod2 = { {s2} };
lnode nod3 = { {s4} };
lnode nod4 = { {s5} };
l->next = &nod1;
nod1.next = &nod2;
q->next = &nod3;
nod3.next = &nod4;
listinsert_L(l, 1, s3);
guibing_L(q, l, p);
print_L(p);
swap_S(s1, s2);
print_S(s1);
return 0;
}
先更新,晚上一会睡前看看,看看有没有什么新理解,不过这玩意应该也没什么可以理解的了