链表(顺序表和单链表)
目录
一.顺序表
#include<iostream>
#include<cstring>
#include<algorithm>
#include<cmath>
#define ll long long
using namespace std;
int len = 10;
struct node
{
string num;
int s;
}a[20];
void creat()
{
cout << "请录入十位同学的学号和成绩信息!" << endl;
for(int i=0;i<10;i++)
{
cin >> a[i].num >> a[i].s;
}
}
void insert(int p) //在位置p插入,p从0开始
{
string ops;
int op;
if(p>len || p<0)
{
cout << "该位置不能插入!" << endl;
return ;
}
cout << "请输入学号和成绩:" << endl;
cin >> ops >> op;
for(int i=len;i>=p;i--)
{
a[i+1].s = a[i].s;
a[i+1].num = a[i].num;
}
a[p].s = op;
a[p].num = ops;
len++;
cout << "插入成功!" << endl;
}
void del(int p) //删除p位置的结点,p从0开始
{
if(p<0 || p>=len)
{
cout << "节点不存在,不能删除!" << endl;
return ;
}
for(int i=p;i<len;i++)
{
a[i].num = a[i+1].num;
a[i].s = a[i+1].s;
}
len--;
cout << "删除成功!" << endl;
}
void change(int p) //修改p节点的信息
{
string ops;
int op;
if(p<0 || p>len)
{
cout << "节点不存在,无法修改!" << endl;
return ;
}
cout << "请输入新的学号和成绩:" << endl;
cin >> ops >> op;
a[p].num = ops;
a[p].s = op;
cout << "修改成功!" << endl;
}
void find(int p)
{
if(p<0 || p>=len)
{
cout << "节点不存在,无法查询!" << endl;
return ;
}
cout << "学号成绩信息如下:" << a[p].num << " " << a[p].s << endl;
}
void print()
{
if(!len)
{
cout << "内容为空!" << endl;
return ;
}
cout << "学好----成绩:" << endl;
for(int i=0;i<len;i++)
cout << "同学" << i << ": " << a[i].num << " " << a[i].s << endl;
}
int main()
{
int tmp,op;
creat();
while(1)
{
cout << "请输入代号" << endl;
cout << "0--退出程序" << endl;
cout << "1--插入信息" << endl;
cout << "2--删除信息" << endl;
cout << "3--修改信息" << endl;
cout << "4--查询信息" << endl;
cin >> tmp;
if(tmp == 0)
return 0;
cout << "请输入位置坐标:" << endl;
cin >> op;
switch(tmp)
{
case 1:insert(op); break;
case 2:del(op); break;
case 3:change(op); break;
case 4:find(op); break;
default:cout << "错误的输入!" << endl;
}
print();
}
return 0;
}
二.单链表
结构体定义中不要使用string类型:关于struct结构体中string的问题_lyc_code的博客-CSDN博客
#include <iostream>
using namespace std;
struct List {
int val;
struct List* next;
};
void Init(struct List *L) { //创建链表
int cur;
cin >> cur;
struct List *ptr,*tail;
tail = L;
while (cur != -1)
{
ptr = (struct List*)malloc(sizeof(struct List));
ptr->val = cur;
ptr->next = NULL;
L->next = ptr;
L = L->next;
cin >> cur;
}
}
void Show(struct List* L) { //遍历链表值
cout << "链表遍历:" ;
while (L->next) {
cout << L->next->val << " ";
L = L->next;
}
cout << endl;
}
//在第K个位置前插入data元素,最后链表 的第K个位置就是data
void InsertList (struct List* L, int k, int data) {
struct List* pre = NULL; //存储第K-1个元素的值
struct List* ptr = (struct List*)malloc(sizeof(struct List)); //申请空间
ptr->val = data;
ptr->next = NULL;
while (k && L->next) { //查找到放置data元素的位置
pre = L;
L = L->next;
k --;
}
if (k > 0) { //如果K > 0 直接插到链表的表尾
L->next = ptr;
L = L->next;
}else {
pre->next = ptr; //链接链表
ptr->next = L;
}
}
int lengthList (struct List* L) { //求链表长度
int len = 0;
while (L->next) {
len ++;
L = L->next;
}
return len;
}
void DeleteList (struct List* L, int x) { //删除值为x的结点(链表无重复值)
if (lengthList(L) <= 0) {
cout << "表空,没有元素可删除" << endl;
return;
}
struct List* ptr = L->next;
struct List* pre = L; //记录ptr的前一个位置的结点
while (ptr) {
if (ptr->val == x) {
pre->next = ptr->next; //把x值的结点的前一个结点的next指向ptr的next结点
free(ptr); //释放空间
return;
}
pre = ptr;
ptr = pre->next;
}
}
void DeleteList_Position(struct List* L, int k) { //删除第K个位置的结点
if (lengthList(L) <= 0) {
cout << "表空,没有元素可删除" << endl;
return;
}
struct List* ptr = L->next;
struct List* pre = L; //记录ptr的前一个位置的结点
k = k - 1; //因为如果k = 1,直接用pre->next = ptr->next就把ptr删掉了,所以要减1
while (k-- && ptr) {
pre = ptr;
ptr = ptr->next;
}
if (ptr == NULL || k > 0) {
cout << "要删除的位置不存在" << endl;
}else {
pre->next = ptr->next; //删除ptr结点
free(ptr); //释放空间
}
}
bool IsEmptyList(struct List* L) { //判断链表是否为空
if (L->next == NULL) {
return true;
}else {
return false;
}
}
int GetElemList(struct List* L, int i) { //返回第i个位置的值
struct List* ptr = L;
int k = i; //标记i的值,以防不存在输出显示
while (i > 0 && ptr->next) {
ptr = ptr->next;
i --;
}
if (i == 0 && ptr != NULL) { //当i == 0 和 ptr 不为空代表找到了第i个位置的元素
return ptr->val;
}else {
cout << "第" << k << "个位置不存在" << endl;
return -1;
}
}
void ClearList(struct List* L) { //清空链表
struct List* ptr = L;
if (lengthList(L) > 0) {
while (ptr->next) {
struct List* temp = ptr->next;
ptr->next = ptr->next->next;
free(temp); //释放空间
}
}
}
int main() {
struct List* head = (struct List*)malloc(sizeof(struct List)); //头结点(不存值)
head->next = NULL;
while(1)
{
cout << "**********************************************" << endl;
cout << "* 1、创建单链表(以-1结束) 2、打印单链表 *" << endl;
cout << "* 3、插入单链表 4、单链表删除 *" << endl;
cout << "* 5、判断是否为空 6、单链表长度 *" << endl;
cout << "* 7、查找 8、退出 *" << endl;
cout << "**********************************************" << endl;
int k;
cout<<"请输入你的选择:";
cin>>k;
switch(k)
{
case 1:
Init(head); //创建单链表
system("pause");
system("cls");
continue;
case 2:
Show(head); //遍历单链表
system("pause");
system("cls");
continue;
case 3:
int i, data;
cout << "请输入要插入的位置和值:";
cin >> i;
cin >> data;
InsertList(head, i, data);
Show(head);
system("pause");
system("cls");
continue;
case 4:
int x;
cout << "请输入要删除的值: ";
cin >> x;
DeleteList(head, x); //删除链表中值为x的结点(链表值无重复)
system("pause");
system("cls");
continue;
case 5:
if (IsEmptyList(head))
cout << "链表是空链表!" << endl;
else
cout << "链表不空!" << endl;
system("pause");
system("cls");
continue;
case 6:
cout << "链表的长度为: " << lengthList(head) << endl;
system("pause");
system("cls");
continue;
case 7:
int n;
cout << "请输入要查找的位置: ";
cin >> n;
if (GetElemList(head, n) != -1)
cout << "第" << n << "个位置的值为: " << GetElemList(head, n) << endl;
system("pause");
system("cls");
continue;
case 8:
break;
default:
cout << "请输入正确的选项!!!" << endl;
system("pause");
system("cls");
continue;
}
system("cls");
break;
}
system("pause");
return 0;
}
#include
#include
#include
#include
#include
#define ll long long
#define len sizeof(book)
using namespace std;
struct book
{
char num[15]; //书号
char name[15]; //书名
char author[15]; //作者
char pub[15]; //出版社
double price;
struct book *next;
};
bool cmp(struct book &x,const book &y)
{
return (atoi(x.num) < atoi(y.num));
}
struct book *creat()
{
struct book *p,*head,*tail;
head = (book *)malloc(len);
head->next = NULL;
tail = head;
for(int i=0;i<10;i++)< p="">
{
p = (book *)malloc(len);
cin >> p->num >> p->name >> p->author >> p->pub >> p->price;
tail->next = p;
tail = p;
tail->next = NULL;
}
return head;
}
void print(struct book *head)
{
struct book *p;
if(head == NULL)
cout << "目录为空" << endl;
else
{
cout << "书号--书名--作者--出版社--价格" << endl;
p = head->next;
while(p != NULL)
{
cout << p-="">num << " " << p-="">name << " " << p-="">author << " " << p-="">pub << " " << p-="">price;
cout << endl;
p = p->next;
}
}
}
void insert(struct book *head,int n) //第n个节点后面插入,n从0开始
{
struct book *p = head,*p0; //p0为插入节点
int i = 0;
p0 = (book *)malloc(len);
cout << "请输入要插入图书的信息" << endl;
cin >> p->num >> p->name >> p->author >> p->pub >> p->price;
while(i < n && p != NULL)
{
i++;
p = p->next;
}
if(p == NULL)
cout << "图书不能插入" << endl;
else
{
p0->next = p->next;
p->next = p0;
print(head);
}
}
void deleteNode(struct book *head,int n) //删除第n个节点,n从1开始
{
struct book *p0 = head,*p = head; //p0为要删除的节点
int i = 0;
while(i < n && p0 != NULL) //p p
{
i++;
p = p0;
p0 = p0->next;
}
if(p0 == NULL)
cout << "不存在" << endl;
else
{
p->next = p0->next;
free(p0);
print(head) ;
}
}
void find(struct book *head,char n[15])
{
struct book *p;
if(head == NULL)
cout << "目录为空,无法查找!" << endl;
else
{
p = head->next;
while(p != NULL && p->num != n)
{
if(strcmp(p->num,n) == 0)
{
cout << "找到了!" << endl;
cout << p-="">num << " " << p-="">name << " " << p-="">author << " " << p-="">pub << " " << p-="">price;
break;
}
p = p->next;
}
if(p == NULL)
cout << "没找到!" << endl;
}
}
void sort(struct book *head)
{
struct book *p = head->next,*q = NULL,*r = NULL; //p指向第一个数据节点
if(p != NULL) //单链表有一个或者以上的数据节点
{
r = p->next; //r 保存 *p节点的直接后继节点的指针
p->next = NULL; //构造只含有一个数据节点的有序表
p = r;
while(p!=NULL)
{
r = p->next; //r 保存*p节点的直接后继节点的指针
q = head;
while(q->next != NULL && (atoi(q->next->num) < atoi(p->num)))
q = q->next; //在有序表中查找插入*p的直接前驱节点*q的位置
p->next = q->next; //将*p插入到*q之后
q->next = p;
p = r; //扫描原单链表余下的节点
}
}
cout << "排序结果如下:" << endl;
print(head);
}
int main()
{
struct book *head;
int n;
char num[15];
head = creat();
print(head);
cout << "请输入要插入图书的位置:" << endl;
cin >> n;
insert(head,n);
cout << "请输入要删除图书的位置:" << endl;
cin >> n;
deleteNode(head,n);
cout << "请输入要查找书名的书号:" << endl;
cin >> num;
find(head,num);
sort(head);
return 0;
}