实验一 线性表的插入和删除

  1. 1、下载文档前请自行甄别文档内容的完整性,平台不提供额外的编辑、内容补充、找答案等附加服务。
  2. 2、"仅部分预览"的文档,不可在线预览部分如存在完整性等问题,可反馈申请退款(可完整预览的文档不适用该条件!)。
  3. 3、如文档侵犯您的权益,请联系客服反馈,我们会尽快为您处理(人工客服工作时间:9:00-18:30)。
cout<<"结点数:"<<count(head)<<"\n";
cout<<"输入要删除结点上的序号!\n";
cin>>num;
head=delete_k_node(head,k);
print(head);
cout<<"输入要删除结点上的整数!\n";
cin>>num;
head=delete_one_node(head,num);
}
//删除链表上第k个结点
node *delete_k_node(node *head,int k)
{
while((find(head,k)==NULL)&&(k!=1))//如果此序号,则重新输入
{
cout<<"该列表无此序号!请重新输入:";
while(!(cin>>k))//输入序号,若不为整数则重输
p1->data=a;
head=insert(head,p1);
cin>>a;
} return(head);
}
//输出链表上各个结点的值
void print(const node *head)
{
const node *p;
p=head;
cout<<"链表上各个结点的数据为:\n";
while(p!=NULL){
p=head;
head=head->next;
delete p;
cout<<"删除了第一个结点!\n";
}
else{
p=find(head,k-1); //查找第k-1个结点,并由p指向该结点
if (p->next!=NULL)
{
p1=p->next;
p->next=p1->next;
delete p1;
}
}
return(head);
}
//删除链表上具有指定值的一个结点
node *delete_one_node(node *head,int num)
{
node *p1,*p2;
if(head==NULL){
cout<<"链表为空,无结点可删!\n";
exit(0);
}
if (head->data==num){
node *find(node *head,int k)
{
if(head==NULL)
{
cout<<"链表为空,无结点可删!\n";
exit(0);
}
if (k<2)
{
return NULL;
}
else
{
node *p;
p=head;
int i;
for(i=2;(i<k)||(p->next==NULL);i++)
print(head);
cout<<"结点数:"<<count(head)<<"\n";
cout<<"输入要删除结点上的序号!\n";
cin>>num;
head=delete_k_node(head,k);
print(head);
cout<<"输入要删除结点上的整数!\n";
cin>>num;
head=delete_one_node(head,num);
print(head);
deletechain(head);
cout<<"输入要插入的整数!\n"
cin>>num;
head=insert(head, num);
print(head);
}
#include <iostream>
#include <iomanip>
using namespace std;
{
node *p1;
while(h){
p1=h;
h=h->next;
delete p1;
}
cout<<"已释放链表的结点空间!\n";
}
int count(node *head)//求链表的结点数
{
int n;
node *p;
p=head;
n=0;
while(p!=NULL){
n=n+1;
p=p->next;
} return(n);
}
//删除链表上第k个结点
node *delete_k_node(node *head,int k)
{
int j=1;
node *p,*p1;
if(head==NULL){
cout<<"链表为空,无结点可删!\n";
return(NULL);
}
p=head;
if (k==1){
{
if(p->next==NULL)
{
return NULL;
}
p=p->next;
}
return p;
}
}
node *insert(node *head,int a)
{
node *p1;
p1=new node;
p1->data=a;
p1->next=NULL;
if(head==NULL)
{
return p1;
print(head);
deletechain(head);
cout<<"输入要插入的整数!\n"
cin>>num;
head=insert(head, num);
print(head);
}
cout<<p->data<<'\t';
p=p->next;
}
cout<<'\n';
}
//删除链表上具有指定值的一个结点
node *delete_one_node(node *head,int num)
{
node *p1,*p2;
if(head==NULL){
cout<<"链表为空,无结点可删!\n";
实验一 线性表的插入和删除
一、实验目的
1、熟悉C++上机环境;
2、掌握线性表的基本操作:查找、插入、删除等运算在链接存储结构上的运
算。
二、实验内容
【任务1】:阅读理解
阅读后面的程序,并将其输入到计算机中,调试成功,运算出结果。这个程序中我们创建了一个整数类型的升序单链表,演示了单链表的创建、输出和删除
{
cerr << "输入错误!请重新输入:";
cin.clear();
cin.sync();
}
}
int j=1;
node *p,*p1;
p=head;
if (k==1){
p=head;
head=head->next;
delete p;
system("cls");
cout<<"成功删除了第一个结点!\n";
p1->next=p2->next;
delete p2;
system("cls");
cout<<"删除了一个结点!\n";
}
else cout<<num<<"链表上没找到要删除的结点!\n";
}
return(head);
}
//求链表的结点数
int count(node *head)
{
int n;
node *p;
}
else
{
node *p2;
p2=head;
if((p2->data)>(p1->data))
{
p1->next=p2;
return p1;
}
while(p2->next!=NULL)
{
if(((p2->data)<=(p1->data))&&((p2->next->data)>=(p1->data)))
struct node{
int data;
node *next;
};
//建立一条有序链表
node *create_sort(void)
{
nodຫໍສະໝຸດ Baidu *p1,*head=0;
int a;
cout<<"建立一条有序链表,请输入数据,以-1结束:";
cin>>a;
while(a!=-1){
p1=new node;
struct node{
int data;
node *next;
};
//建立一条升序单链表
node *create_sort(void)
{
node *head=NULL;
int a;
cout<<"建立一条有序链表,请输入数据,以-1结束:\n";
while(!(cin>>a))//输入整数,若不为整数则重输
{
p1->next=p2->next;
p2->next=p1;
return head;
}
p2=p2->next;
}
p2->next=p1;
return head;
}
}
void main(void)
{
node *head;
int num;
int k;
head= create_sort ();
print(head);
}
if (p2->data==num){
p1->next=p2->next;
delete p2;
cout<<"删除了一个结点!\n";
}
else cout<<num<<"链表上没找到要删除的结点!\n";
}
return(head);
}
//释放链表的结点空间
void deletechain(node *h)
return(NULL);
}
if (head->data==num){
p1=head;
head=head->next;
delete p1;
cout<<"删除了一个结点!\n";
}
else{
p2=p1=head;
while(p2->data!=num&&p2->next!=NULL){
p1=p2;
p2=p2->next;
p=head;
n=0;
while(p!=NULL){
n=n+1;
p=p->next;
}
return(n);
}
//释放链表的结点空间
void deletechain(node *h)
{
node *p1;
while(h){
p1=h;
h=h->next;
delete p1;
}
cout<<"已释放链表的结点空间!\n";
p1=head;
head=head->next;
delete p1;
system("cls");
cout<<"删除了一个结点!\n";
}
else{
p2=p1=head;
while(p2->data!=num&&p2->next!=NULL){
p1=p2;
p2=p2->next;
}
if (p2->data==num){
}
else{
p=find(head,k); //查找第k-1个结点,并由p指向该结点
p1=p->next;
p->next=p1->next;
delete p1;
system("cls");
cout<<"成功删除了第"<<k<<"个结点!\n";
}
return(head);
}
//查找第K个节点,返回该节点前一个节点的地址,否则返回NULL
cout<<"删除了第"<<k<<"个结点!\n";
}
return(head);
}
//函数insert(node *head,int num)实现把一个结点插入链表,仍保持链表上各结点的升序关系,你自己实现
void main(void)
{
node *head;
int num;
int k;
head= create_sort ();
{
cerr << "输入错误!请重新输入:";
cin.clear();
cin.sync();
}
while(a!=-1)
{
head=insert(head,a);
while(!(cin>>a))//输入整数,若不为整数则重输
{
cerr << "输入错误!请重新输入:";
cin.clear();
cin.sync();
操作。
【任务2】:完善功能
构造函数node *insert(node *head,int num),实现把一个结点插入链表,仍保持链表上各结点的升序关系,并在主函数中完成对你所添加函数的测试。
三、算法描述
建立含有若干个元素的升序单链表,对其进行插入、删除等操作,并将结果在屏幕上输出。
#include <iostream.h>
相关文档
最新文档