实验性质
设计性实验
要求
(1)编程实现单链表的以下基本操作:建立单链表,查找单链表,插入单链表,删除单链表。
(2)采用单链表结构编程实现:两个有序单链表的归并运算。
目的
(1)掌握线性表的链式存储结构;
(2)掌握单链表及其基本操作的实现。
先上图!!!
源码如下:
#include <iostream>
#include <stdio.h>
#include <stdlib.h>
using namespace std;
//单链表节点定义
typedef struct LNode
{
int data;
struct LNode *next;
}LNode,*LinkList;
LinkList L;
//1.前插法创建链表
void CreatList_head(LinkList &L , int n)
{
cout <<"请依次换行输入"<<n<<"个数据!"<<endl;
L = (LinkList)malloc(sizeof(LNode));
L ->next =NULL; //初始化单链表
LNode *s ; //声明一个中间变量
for(int i=0 ;i<n;i++)
{
s = (LNode*)malloc(sizeof(LNode)); //生成一个新节点
cin >> s ->data ;
s->next = L->next; //新结点的next指针指向开始结点
L->next = s; //头结点的next指针指向新结点
}
cout <<"创建成功!!!"<<endl;
}
//2.尾插法创建链表
void CreatList_tail(LinkList &L , int n)
{
cout <<"请依次换行输入"<<n<<"个数据!"<<endl;
L = (LinkList)malloc(sizeof(LNode));
L->next = NULL;
LNode *s;
LNode * r = L;
for(int i=0 ;i< n;i++)
{
s = (LNode*)malloc(sizeof(LNode));
cin >> s->data;
r->next = s;
r = s;
}
r->next = NULL;
cout <<"创建成功!!!"<<endl;
}
//3.按照位置取值
void GetElem(LinkList &L ,int place)
{
LNode *node = L;
int j = 0;
while(node&&j<place)
{
node = node ->next;
j = j+1;
}
if(!node ||j > place)
{
cout <<"输入的值有问题!!!"<<endl;
}
else
{
cout<<"该位置上的值为"<< node->data <<endl;
}
}
//4.按照值查找
int Getbyzhi(LinkList &L,int elem)
{
LNode *temp = L;
int i = 0;
while(temp->next)
{
i=i+1;
temp = temp->next;
if(elem == temp->data)
{
cout << "该值在链表中的第"<<i<<"位"<<endl;
return 0;
}
}
cout << "查找失败!!!"<<endl;
return 0;
}
//5.插入元素
void Insert(LinkList &L, int place1,int elem)
{
LNode *p = L;
int j = 0;
while(p&&(j<place1-1))
{
p = p->next;
j++;
}
if(p == NULL)
{
cout << "插入失败!!!"<<endl;
}
else
{
LNode *s = (LNode*)malloc(sizeof(LNode));
s->data = elem;
s->next = p->next;
p->next = s;
cout << "插入成功!!!"<<endl;
}
}
//删除
void DeleteElem(LinkList &L ,int place3)
{
LNode *p = L;
int j=0;
while(p&&j <place3-1)
{
p = p->next;
j++;
}
if(p == NULL)
{
cout << "删除失败!!!"<<endl;
}
else
{
LNode *newone = p->next;
p->next = newone->next;
free(newone);
cout << "删除成功!!!"<<endl;
}
}
//打印链表
void print(LinkList &L)
{
LNode *node = L->next;
cout << "表中元素为:";
if(node == NULL)
{
cout << " 该表为空!!!"<<endl;
}
else
{
while (node != NULL)
{
cout << " "<<node->data << " ";
node = node->next;
}
cout << endl;
}
}
void Message()
{
cout <<"1.前插法创建链表"<<endl;
cout <<"2.尾插法创建链表"<<endl;
cout <<"3.按照位置取值"<<endl;
cout <<"4.按照值查位置"<<endl;
cout <<"5.插入元素"<<endl;
cout <<"6.删除元素"<<endl;
cout << "7.打印链表"<<endl;
cout <<"输入负数退出!!!"<<endl;
}
void switchone(int number)
{
switch(number)
{
case 1:
cout << "请输入要创建链表的长度:";
int length;
cin >> length;
CreatList_head(L,length);
break;
case 2:
cout << "请输入要创建链表的长度:";
int length1;
cin >> length1;
CreatList_tail(L,length1);
break;
case 3:
cout << "请输入位置:";
int placeone;
cin >> placeone ;
GetElem(L,placeone);
break;
case 4:
cout << "请输入值:";
int zhi;
cin >> zhi ;
Getbyzhi(L,zhi);
break;
case 5:
cout <<"请输入插入位置:";
int placetwo;
cin >> placetwo;
cout << "请输入插入的值:";
int elem1;
cin >> elem1;
Insert(L,placetwo,elem1);
break;
case 6:
cout << "请输入要删除的位置:";
int place12;
cin >> place12;
DeleteElem(L,place12);
break;
case 7:
print(L);
break;
default :
cout << "输入有误,请重新输入!!!"<< endl;
}
}
int main()
{
Message();
int number;
cout << "请输入操作代码:";
cin >> number;
while(true)
{
if(number<0)
{
break;
}
else
{
switchone(number);
}
cout << "请输入操作代码:";
cin >> number;
}
return 0;
}