#include <iostream>
using namespace std;
int main()
{
struct Students//定义结构体
{
int id;
char names[100];
char scores[100];
Students* next;//使用类指针时要在类的构建时声明类的指针,即保存一个指针而已
};
Students ss[4] =//定义类型的四个实例
{
{1,"码云","100"},
{2,"麻花疼","90"},
{3,"武延著","80"},
{4,"温柔","70"}
};
//制作链表
ss[0].next = &ss[1];//将声明的类指针指向下一个链表地址
ss[1].next = &ss[2];
ss[2].next = &ss[3];
ss[3].next = NULL;
Students* ptr = &ss[0];//指针指向第一个链表的地址
while (ptr)
{
cout << ptr->id << endl;
ptr = ptr->next;
};
return 0;
}
//在调试的时候还可以明显的看到链表的结构
#include <iostream>
using namespace std;
struct Students//定义结构体
{
int id;
char names[100];
char scores[100];
Students* next;//使用类指针时要在类的构建时声明类的指针
};
Students ss[4] =//定义类型的四个实例
{
{20190401,"码云","100"},
{20190402,"麻花疼","90"},
{20190403,"武延著","80"},
{20190404,"温柔","70"}
};
Students* find(Students* ptr_head, int input_id)//定义一个查找函数,根据id输出学生的信息.返回值类型为指针
{
Students* ptr = ptr_head;//类指针指向这个输入的链表的地址
while (ptr)
{
if (ptr->id == input_id)
{
return ptr;
//cout << ptr->names << "\t" << ptr->scores << endl;
//break;
}
ptr = ptr->next;
}
return NULL;
}
int main()
{
//制作链表
ss[0].next = &ss[1];//将声明的类指针指向下一个链表地址
ss[1].next = &ss[2];
ss[2].next = &ss[3];
ss[3].next = NULL;
Students* ptr = &ss[0];//指针指向第一个链表的地址
while (ptr)
{
cout << ptr->names << "\t" << ptr-> scores << endl;
ptr = ptr->next;
};
Students* ptr2 = find(&ss[0], 20190402);//返回的是一个指针,所以要通过指针调用成员变量
cout << "查找的用户信息为:" << ptr2->names << "\t" << ptr2->scores << endl;
return 0;
}
//程序输出
码云 100
麻花疼 90
武延著 80
温柔 70
查找的用户信息为:麻花疼 90
C:\Users\C_plus_test\constructdemo\Debug\constructdemo.exe (进程 188688)已退出,返回代码为: 0。
若要在调试停止时自动关闭控制台,请启用“工具”->“选项”->“调试”->“调试停止时自动关闭控制台”。
按任意键关闭此窗口...
固定一个头链表
添加一个链表元素
#include <iostream>
using namespace std;
#include <string.h> //使用strcpy_s函数要用到#include <string.h>#include <stdio.h>两个头文件
#include <stdio.h>
struct Students//定义结构体
{
int id;
char names[100];
char scores[100];
Students* next;//使用类指针时要在类的构建时声明类的指针
};
Students ss[4] =//定义类型的四个实例
{
{20190401,"码云","100"},
{20190402,"麻花疼","90"},
{20190403,"武延著","80"},
{20190404,"温柔","70"}
};
int main()
{
//链表,一般首先创建一个固定的头链表
Students head = { 0 };//一个空的类型,没有存数据,作为这个链表的头
Students* ptr_head = &head;
ptr_head->next = &ss[0];
ss[0].next = &ss[1];
ss[1].next = &ss[2];
ss[2].next = &ss[3];
ss[3].next = NULL;
//上面的链表已经构建完成,下面开始在表头插入一个表
//首先动态构建一个表
Students* ptr_obj1 = new Students();//使用堆生成一块内存动态的存储表
ptr_obj1->id = 20190405;
strcpy_s(ptr_obj1->names, "阿狸晕");
strcpy_s(ptr_obj1->scores, "100");
Students* ptr_obj2 = new Students();//使用堆生成一块内存动态的存储表
ptr_obj2->id = 20190406;
strcpy_s(ptr_obj2->names, "疼训晕");
strcpy_s(ptr_obj2->scores, "89");
//在表头插入这个表,让插入表指向头表的指向,然后再让头表指向插入的表
ptr_obj1->next = ptr_head->next;
ptr_head->next = ptr_obj1;
//在表尾插入一个表,先让最后一个指向要插入的表,再将插入的那个表指向null
Students* ptr = ptr_head;
while (ptr->next)//这里判断标准是最后一个表,要用ptr->next,而非ptr,如果ptr是最后一个,那么它所指向的是NULL—>next = ptr_obj2就会进行报错
{
ptr = ptr->next;
}
ptr->next = ptr_obj2;
ptr_obj2->next = NULL;
//将数据封装起来,加入用户输入的部分
#include <iostream>
using namespace std;
#include <string.h> //使用strcpy_s函数要用到#include <string.h>#include <stdio.h>两个头文件
#include <stdio.h>
struct Students//定义结构体
{
int id;
char names[100];
char scores[100];
Students* next;//使用类指针时要在类的构建时声明类的指针
};
Students ss[4] =//定义类型的四个实例
{
{20190401,"码云","100"},
{20190402,"麻花疼","90"},
{20190403,"武延著","80"},
{20190404,"温柔","70"}
};
//链表,一般首先创建一个固定的头链表
Students head = { 0 };//一个空的类型,没有存数据,作为这个链表的头
Students* ptr_head = &head;
//链表首插入
void add_head(Students*);
void add_head(Students* ptr_obj)
{
ptr_obj->next = ptr_head->next;
ptr_head->next = ptr_obj;
}
//链表尾部插入
void add_tail(Students*);
void add_tail(Students* ptr_obj)//
{
Students* ptr = &head;//最好用全局变量,不然容易过载,自己都不知道那是啥
while (ptr->next)//判断是否是尾指针
{
ptr = ptr->next;
}
ptr->next = ptr_obj;//进行尾指针的重新指向
ptr_obj->next = NULL;
}
//定义一个查找函数,根据id输出学生的信息.返回值类型为指针
Students* find(Students* ptr_head, int input_id)
{
Students* ptr = ptr_head;//类指针指向这个输入的链表的地址
while (ptr)
{
if (ptr->id == input_id)
{
return ptr;
//cout << ptr->names << "\t" << ptr->scores << endl;
//break;
}
ptr = ptr->next;
}
return NULL;
}
//读取用户输入
int read_input(const Students* );
int read_input(const Students* ptr_obj)
{
cout << "id: ";
scanf_s("%d",&ptr_obj->id);
cout << "name: ";
scanf_s("%s", &ptr_obj->names,100);//这里一定要记得写上范围100,不然会报错
cout << "score: ";
scanf_s("%s",&ptr_obj->scores,100);
return 0;
}
//打印链表
void showinfo();
void showinfo(Students* ptr_show)
{
//Students* ptr = ptr_show->next;//这样比较乱,最好直接用元数据
Students* ptr = head.next;
while (ptr)
{
cout << "id: " << ptr->id << "\t" << "name: " << ptr->names << "\t" << "scores: " << ptr->scores << endl;
ptr = ptr->next;
}
}
int main()
{
//构建链表
ptr_head->next = &ss[0];
ss[0].next = &ss[1];
ss[1].next = &ss[2];
ss[2].next = &ss[3];
ss[3].next = NULL;
//上面的链表已经构建完成,下面开始在表头插入一个表
//首先动态构建一个表
Students* ptr_obj1 = new Students();//使用堆生成一块内存动态的存储表
read_input(ptr_obj1);
Students* ptr_obj2 = new Students();//使用堆生成一块内存动态的存储表
read_input(ptr_obj2);
//在表头插入这个表
add_head(ptr_obj1);
//在表尾插入一个表
add_tail(ptr_obj2);
//打印链表
showinfo(&head);
//释放堆内存
delete ptr_obj1;
delete ptr_obj2;
return 0;
}
//程序输出
id: 20190407
name: 六强冬
score: 59
id: 20190408
name: 顶雷
score: 59
id: 20190407 name: 六强冬 scores: 59
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190403 name: 武延著 scores: 80
id: 20190404 name: 温柔 scores: 70
id: 20190408 name: 顶雷 scores: 59
插入一个表
1、找到要插入的节点pre以后,就可以在结果点后面进行插入操作
2、关键点是一定要另记录上一个节点,因为通过当前节点cur判断,满足条件是是通过前一个节点进行操作的
#include <iostream>
using namespace std;
#include <string.h> //使用strcpy_s函数要用到#include <string.h>#include <stdio.h>两个头文件
#include <stdio.h>
struct Students//定义结构体
{
int id;
char names[100];
char scores[100];
Students* next;//使用类指针时要在类的构建时声明类的指针
};
Students ss[4] =//定义类型的四个实例
{
{20190401,"码云","100"},
{20190402,"麻花疼","90"},
{20190403,"武延著","80"},
{20190404,"温柔","70"}
};
//按照id顺序插入节点
int insert_num(Students*);
int insert_num(Students* ptr_head,Students* ptr_new)//参数:原链表头指针、要插入表的指针
{
//取出原链表的头指针
Students* ptr_cur = ptr_head->next;
Students* ptr_pre = ptr_head;//记录前一个节点非常重要
while (ptr_cur)//如果不是尾指针
{
//找到插入位置
if (ptr_new->id < ptr_cur->id)
{
break;
}
ptr_pre = ptr_cur;
ptr_cur = ptr_cur->next;
}
//跳出while循环时ptr_table->next是NULL,说明ptr_table是尾指针,进行ptr_table指针的重新指向
ptr_new->next = ptr_pre->next;
ptr_pre->next = ptr_new;
return 0;
}
//打印链表
void showinfo();
void showinfo(Students* ptr_show)
{
//Students* ptr = ptr_show->next;//这样比较乱,最好直接用元数据
Students* ptr = head.next;
while (ptr)
{
cout << "id: " << ptr->id << "\t" << "name: " << ptr->names << "\t" << "scores: " << ptr->scores << endl;
ptr = ptr->next;
}
}
int main()
{
//构建链表
ptr_head->next = &ss[0];
ss[0].next = &ss[1];
ss[1].next = &ss[2];
ss[2].next = &ss[3];
ss[3].next = NULL;
showinfo(&head);
cout << "---------------------" << endl;
Students kiki = {20190402,"层隆","10"};
Students* ptr_kiki = &kiki;
insert_num(&head, &kiki);
showinfo(&head);
return 0;
}
//程序输出
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190403 name: 武延著 scores: 80
id: 20190404 name: 温柔 scores: 70
---------------------
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190402 name: 层隆 scores: 10
id: 20190403 name: 武延著 scores: 80
id: 20190404 name: 温柔 scores: 70
同理删除一个节点,也是通过前一个节点pre进行操作的
pre->next = obj->next;
free(obj);
int remove_num(Students*, int);
int remove_num(Students* ptr_head, int id)//参数:原链表头指针、要插入表的指针
{
//取出原链表的头指针
Students* ptr_cur = ptr_head->next;
Students* ptr_pre = ptr_head;
while (ptr_cur)//如果不是尾指针
{
//判断是否是要找的节点id
if (ptr_cur->id == id)
{
break;
}
ptr_pre = ptr_cur;
ptr_cur = ptr_cur->next;
}
//找到节点后,跳出while循环,对这个节点的前一个节点,进行重新指向操作
ptr_pre->next = ptr_cur->next;
free(ptr_cur);
return 0;//若是返回零的话,说明成功删除
}
int main()
{ //主函数省略了上面的一些代码
cout << "---------------------" << endl;
//删除一个实例
remove_num(&head, 20190403);
showinfo(&head);
//释放堆内存
delete ptr_obj1;
delete ptr_obj2;
return 0;
}
//程序输出
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190403 name: 武延著 scores: 80
id: 20190404 name: 温柔 scores: 70
---------------------
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190402 name: 层隆 scores: 10
id: 20190403 name: 武延著 scores: 80
id: 20190404 name: 温柔 scores: 70
---------------------
id: 20190401 name: 码云 scores: 100
id: 20190402 name: 麻花疼 scores: 90
id: 20190402 name: 层隆 scores: 10
id: 20190404 name: 温柔 scores: 70
Process returned 0 (0x0) execution time : 0.194 s
Press any key to continue.