数据结构第一次上机实验
!!!运行环境:Dev c++ 5.11!!!
!!!运行环境:Dev c++ 5.11!!!
!!!运行环境:Dev c++ 5.11!!!
!!!运行环境:Dev c++ 5.11!!!
心得
1,malloc函数需要头文件#include<malloc.h>
2,函数需要多多复习
3,凑数的
4,如有错误请指正,劳烦赐教
5,数据结构有点难
题目如下
这一次上机一共包括两个程序
1,顺序表的基本操作
2,单链表的基本操作
第一题与实现代码
1.编写程序,实现顺序表的基本运算,并在此基础上设计一个主程序完成如下功能。
(1)初始化顺序表L
(2)依次插入a,b,c,d,e元素
(3)输出顺序表L
(4)输出顺序表L长度
(5)判断顺序表L是否为空
(6)输出顺序表L的第3个元素
(7)输出元素‘a’的位置
(8)在第4个位置上插入‘f’元素
(9)输出顺序表L
(10)删除L的第3个元素
(11)输出顺序表
(12)释放顺序表L
#include<iostream>
#include<malloc.h>
using namespace std;
typedef struct{
char data[10];
int len;
}SqList;
void InitList(SqList *&L) //初始化
{
L=(SqList *)malloc(sizeof(SqList));
L->len=0;
}
void input(SqList *&L) //赋值
{
int i=0,k=0,n=5;
char a[n]={'a','b','c','d','e'};
while(i<n)
{
L->data[i]=a[i];
i++;k++;
}
L->len=k;
}
void DispList(SqList *L) //输出
{
cout<<"输出顺序表L:";
for(int i=0;i<L->len;i++)
cout<<L->data[i];
cout<<endl;
}
bool GetElem(SqList *L,int i,char &e) //输出指定元素
{
if(i<0||i>L->len)
return false;
e=L->data[i-1];
return true;
}
int LocateElem(SqList *L,char e) //查找元素位置
{
for(int i=0;i<L->len;i++)
if(L->data[i]==e)
return(i+1);
return 0 ;
}
bool ListInsert(SqList*&L,int i,char e) //插入元素
{
if(i<0||i>L->len)
return false;
i--;
for(int x=L->len;x>i;x--)
L->data[x]=L->data[x-1];
L->data[i]=e;
L->len++;
cout<<'\n';
return true;
}
bool ListDelete(SqList *&L,int i) //删除元素
{
if(i<0||i>L->len)
return false;
i--;
for(int j=i;j>L->len;j++)
L->data[j]=L->data[j+1];
L->len--;
return true;
}
void FreeList(SqList*&L) //释放顺序表
{
free(L);
cout<<"顺序表释放成功";
}
int main()
{
SqList *L;
InitList(L);
input(L);
DispList(L);
cout<<"顺序表L长度:"<<L->len<<endl; //顺序表长度
if(L==NULL) //判断顺序表是否为空
cout<<"顺序表为空"<<endl;
else
cout<<"顺序表不为空" <<endl;
char e;
GetElem(L,3,e);
cout<<"第三个元素是"<<e<<endl;
e='a';
int a; a=LocateElem(L,e);
cout<<"a在第"<<a<<"个位置";
e='f';
ListInsert(L,4,e);
DispList(L);
ListDelete(L,3);
DispList(L);
FreeList(L);
return 0;
}
第二题及实现代码
2.编写程序,实现单链表的基本运算,并在此基础上设计一个主程序完成如下功能。
(1)初始化单链表h
(2)采用尾插法依次插入a,b,c,d,e元素
(3)输出单链表h
(4) 输出单链表h长度
(5)判断单链表h是否为空
(6)输出单链表h的第3个元素
(7)输出元素‘a’的位置
(8)在第4个元素位置上插入‘f’元素
(9)输出单链表h
(10)删除h的第3个元素
(11)输出单链表h
(12)释放单链表h
#include<iostream>
using namespace std;
struct node{
char data;
node *next;
};
void InitList(node *&L) //初始化
{
L=new node;
L->next=NULL;
}
void CreatList(node *&L,int n) //尾插法
{
node *p,*s;
p=L;
char e[]={'a','b','c','d','e'};
for(int i=0;i<n;i++)
{
node *s=new node;
s->data=e[i];
p->next=s;
p=s;
}
p->next=NULL;
}
void DispList(node *L) //输出
{
node *p=L->next;
cout<<"单链表为:";
while(p!=NULL)
{
cout<<p->data;
p=p->next;
}
cout<<endl;
}
void ListLength(node*L) //求长度
{
int i=0;
node *p=L;
while(p->next!=NULL)
{
i++;
p=p->next;
}
cout<<"单链表长度为:"<<i<<endl;
}
bool GetElem(node *L,int i,char &e) //输出指定元素
{
int j=0;
node *p=L;
if(i<=0)
return false;
while(j<i&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
e=p->data;
return true;
}
}
int LocateElem(node*L,char e) //输出指定元素的位置
{
int i=1;
node *p=L->next;
while(p->data!=e&&p!=NULL)
{
i++;
p=p->next;
}
if(p==NULL)
return 0;
else
return i;
}
bool ListInsert(node*&L,int i,char e) //插入元素
{
int j=0;
node *p=L,*s;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=new node;
s->data=e;
s->next=p->next;
p->next=s;
return true;
}
}
bool ListDelete(node*&L,int i) //删除元素
{
node *p=L,*s;
int j=0;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return false;
else
{
s=p->next;
if(s==NULL)
return false;
p->next=s->next;
delete s;
return true;
}
}
void DistoryList(node*&L) //销毁
{
node *p=L,*s=L->next;
while(s!=NULL)
{
delete p;
p=s;
s=s->next;
}
delete s;
cout<<"销毁成功"<<endl;
}
int main()
{
node *L;
InitList(L);
CreatList(L,5);
DispList(L);
ListLength(L);
if(L->next==NULL)
cout<<"单链表为空"<<endl;
else
cout<<"单链表不为空"<<endl;
char e;
int j=3;
GetElem(L,j,e);
cout<<"单链表第三个元素是"<<e<<endl;
e='a';
int i=LocateElem(L,e);
cout<<"元素'a'的位置是"<<i<<endl;
j=4; e='f';
ListInsert(L,j,e);
DispList(L);
j=3;
ListDelete(L,j);
DispList(L);
DistoryList(L);
return 0;
}