線性表的順序存儲實驗
❶ 數據結構實驗(C語言): 順序表實驗
//線性表函數操作
#include <stdio.h>
#include <string.h>
#define MaxSize 30
#define Error 0
#define True 1
typedef char ElemType;
typedef struct
{
ElemType elem[MaxSize];
int length;
}SqList; /*順序表類型定義*/
void InitList(SqList * &L) /*初始化順序表L*/
{
L = (SqList *)malloc(sizeof(SqList));
L -> length = 0;
}
void DestroyList( SqList *L ) /*釋放順序表L*/
{
free(L);
}
int ListEmpty( SqList *L ) /*判斷順序表L是否為空表*/
{
return( L -> length == 0);
}
int ListLength( SqList *L ) /*返回順序表L的元素個數*/
{
return( L -> length);
}
void DispList( SqList *L ) /*輸出順序表L*/
{
int i;
if( ListEmpty(L))
return;
for( i = 0; i < L -> length; i++ )
printf("%c", L -> elem[i]);
printf("\n");
}
int GetElem( SqList *L, int i, ElemType &e) /*獲取順序表中的第i個元素*/
{
if( i < 1 || i > L -> elem[i])
return Error;
e = L -> elem[i - 1];
return True;
}
int LocateElem( SqList *L, ElemType e) /*在順序表中查找元素e*/
{
int i = 0;
while( i < L -> length && L -> elem[i] != e)
i++;
if(i >= L -> length)
return Error;
else
return i+1;
}
int ListInsert( SqList * &L, int i, ElemType e) /*在順序表L中第i個位置插入元素e*/
{
int j;
if( i < 1 || i > L -> length + 1)
return 0;
i--; /*將順序表位序轉化為elem下標*/
for( j = L -> length; j > i; j--) /*將elem[i]及後面元素後移一個位置*/
L -> elem[j] = L -> elem[j - 1];
L -> elem[i] = e;
L -> length++; /*順序表長度增1*/
return True;
}
int ListDelete( SqList * &L, int i, ElemType &e) /*順序表L中刪除第i個元素*/
{
int j;
if( i < 1 || i > L -> length)
return Error;
i--; /*將順序表位序轉化為elem下標*/
e = L -> elem[i];
for(j = i; j < L -> length - i; j++)
L -> elem[j] = L -> elem[j + 1];
L -> length--; /*順序表長度減1*/
return True;
}
void main()
{
SqList *L;
ElemType e;
printf("(1)初始化順序表L\n");
InitList(L);
printf("(2)依次採用尾插法插入a,b,c,d,e元素\n");
ListInsert(L, 1, 'a');
ListInsert(L, 2, 'b');
ListInsert(L, 3, 'c');
ListInsert(L, 4, 'd');
ListInsert(L, 5, 'e');
printf("(3)輸出順序表L:");
DispList(L);
printf("(4)順序表L長度 = %d\n", ListLength(L));
printf("(5)順序表L為%s\n", (ListEmpty(L) ?"空" :"非空"));
GetElem(L, 3, e);
printf("(6)順序表L的第3個元素 = %c\n", e);
printf("(7)元素a的位置 = %d\n", LocateElem(L,'a'));
printf("(8)在第4個元素位置上插入f元素\n");
ListInsert(L, 4, 'f');
printf("(9)輸出新的順序表L:");
DispList(L);
printf("(10)刪除L的第3個元素\n");
ListDelete(L, 3, e);
printf("(11)輸出新的順序表L:");
DispList(L);
printf("(12)釋放順序表L\n");
DestroyList(L);
}
❷ 順序表的基本操作實驗總結
順序表的基本操作實驗總結:
順序表是在計算機內存中以數組的形式保存的線性表,線性表的順序存儲是指用一組地址連續的存儲單元依次存儲線性表中的各個元素、使得線性表中在邏輯結構上相鄰的數據元素存儲在相鄰的物理存儲單元中,
即通過數據元素物理存儲的相鄰關系來反映數據元素之間邏輯上的相鄰關系,採用順序存儲結構的線性表通常稱為順序表。順序表是將表中的結點依次存放在計算機內存中一組地址連續的存儲單元中。
將表中元素一個接一個的存入一組連續的存儲單元中,這種存儲結構是順序結構。採用順序存儲結構的線性表簡稱為「 順序表」。順序表的存儲特點是:只要確定了起始位置,表中任一元素的地址都通過下列公式得到:LOC(ai)=LOC(a1)+(i-1)*L 1≤i≤n
其中,L是元素佔用存儲單元的長度。如順序表的每個結點佔用len個內存單元,用location (ki)表示順序表中第i個結點ki所佔內存空間的第1個單元的地址。則有如下的關系:location (ki+1) = location (ki) +len
❸ 線性表的順序存儲實驗
你改下下面代碼的標點就行了:
#include<iostream.h>
#define elemtype int
struct link
{ elemtype data;//元素類型
link *next; //指針類型,存放下一個元素地址
};
//頭插法建立帶頭結點的單鏈表
link *hcreat()
{ link s,p;
elemtype i;
cout<<」輸入多個結點數值(用空格分隔),為0時演算法結束」;
cin>>i;
p=new link;
p->next=NULL;
while(i) //當輸入的數據不為0時,循環建單鏈表
{s=new link;
s->data=i;
s->next=p->next;
p->next=s;
cin>>i; }
return p;
}
//輸出單鏈表
void print(1ink *head)
{
1ink *p;
p=head->next;
while(p->next!=NULL)
{
cout<<p->data<<」->」; //輸出表中非最後一個元素
p=p->next;
}
cout<<p->data; //輸出表中最後一個元素
cout<<endl;
}
∥在單鏈表head中查找值為x的結點
Link *Locate(1ink *head,elemtype x)
{
Link *p;
p=head->next;
while((p!=NULL)&&(p->data!=x))
p=p->next;
return p; }
//在head為頭指針的單鏈表中,刪除值為x的結點
void deletel(1ink *head,elemtype x)
{
1ink *p, *q;
q=head;
p=head->next;
while((p!=NULL)&&(p->data!=x))
{
q=p;
p=p->next;}
If(p==NULL) cout<<「要刪除的結點不存在」;
else
q->next=p ->next;
delete(p);
}
}
//在頭指針head所指的單鏈表中,在值為x的結點之後插入值為y的結點
void insert(1ink *head,elemtype x,elemtype y)
{ link *p, *s;
s=new link;
s->data=y;
if(head->next==NULL) //鏈表為空
{
head->next=s;
s->next=NULL:
}
p=Locate(head,x);//調用查找演算法 『
if(p==NULL)
cout<<」插入位置非法」:
else
(s->next=p->next;
p->next=s;
}
}
//將單鏈表p中所有值為x的元素修改成y
void change(1ink *p,elemtype x,elemtype y)
{
link *q;
q=p->next;
while(q!=NULL)
{ if(q->data==x) q->data=y;
q=q->next;
}
}
void count(1ink *h) //統計單鏈表中結點個數
{
1ink *p;int n=0;
p=h->next;
while(p!=NULL)
{n++;p=p->next;}
return n;
}
void main()
{ int n;
elemtype x,y;
link *p, *q;
p=hcreat(); //頭插法建立鏈表
print(p); //輸出剛建立的單鏈表
cout<<」請輸入要刪除的元素」;
cin>>y;
deletel(p,y);
print(p); //輸出刪除後的結果
cout<<」請輸入插入位置的元素值(將待插元素插入到它的後面)」;
cin>>x;
cout<<」請輸入待插元素值」;
cin>>y;
insert(p,x,y);
print(p); //輸出插入後的結果
cout<<」請輸入要修改前、後的元素值」;
cin>>x>>y;
change(p,x,y);
print(p);
cout<<」請輸入要查找的元素值」;
cin>>x;
q=Locate(p,x);
if(q==NULL)cout<<x<<」不在表中,找不到!」<<endl;
else cout<<x<<」在表中,已找到!」<<endl;
n=count(p);
cout<<」鏈表中結點個數為:」<<n<<endl:
}
❹ 數據結構實驗:線性表順序存儲和鏈式存儲(簡單鏈表)插入、刪除運算
#include <iostream.h>
#include <iomanip.h>
typedef struct Lnode
{
int data;
struct Lnode *next;
}LNode,*LinkList;
void CreateList1(LinkList &L,int n);//insert from head
void CreateList2(LinkList &L,int n);//insert from tail
void CreateList3(LinkList &L,int n);//insert by sorting number
void InsertList(LinkList L);
void DeleteList(LinkList L,int num);
void ShowList(LinkList L);
void FreeList(LinkList L);
void main()
{
LinkList H;
int num;
/*
//reverse order input
cout<<"Input the length of list:"<<endl;
cin>>num;
CreateList1(H,num);
cout<<"The List is:";
ShowList(H);
FreeList(H);*/
cout<<"Input the length of list:"<<endl;
cin>>num;
CreateList3(H,num);
cout<<"The List is:";
ShowList(H);
/*InsertList(H);
cout<<"The new List is:";
ShowList(H);
cout<<"the delete data:";
cin>>num;
DeleteList(H,num);
cout<<"The new List is:";
ShowList(H);*/
FreeList(H);
}
//insert from the head of list when creating
void CreateList1(LinkList &L,int n)
{
LinkList p;
int i;
L=new LNode;
L->next =NULL;
cout<<"input the data of list (in reverse order)!"<<endl ;
for(i=0;i<n;i++)
{
p=new LNode;
cin>>p->data;
p->next=L->next;
L->next =p;
}
}
//insert from the tail of list when creating
void CreateList2(LinkList &L,int n)
{
LinkList p;
int i;
L=new LNode;
L->next =NULL;
LinkList head=L;
cout<<"input the data of list (in order)!"<<endl ;
for(i=0;i<n;i++)
{
p=new LNode;
cin>>p->data;
head->next=p;
p->next=NULL;
head=head->next;
}
}
//insert List by sorting number
void CreateList3(LinkList &L,int n){
LinkList p,q;
L=new LNode;
L->next =NULL;
cout<<"input the data of list (It can autoorder from small to big)!"<<endl ;
for(int i=0;i<n;i++)
{
q=L;
p=new LNode;
cin>>p->data;
while((q->next!=NULL)&&(p->data>q->next->data)){//be careful for condition
//from the condition,we can know runing principle for "&&"
q=q->next;
}
p->next=q->next;
q->next =p;
}
}
void ShowList(LinkList L)
{
LinkList p;
for(p=L->next; p;p=p->next)
cout<<setw(5)<<p->data<<" ";
cout<<endl;
}
//insert data from tail
void InsertList(LinkList L){
LinkList p=L,q;
q=new LNode;
cout<<"input the inserting number :";
cin>>q->data;
while(p->next){
p=p->next;
}
p->next=q;
q->next=NULL;
}
//delete data(all input data)
void DeleteList(LinkList L,int data){
LinkList p=L,q;
while(p->next){
q=p ;
p=p->next;
if(p->data==data){
q->next=p->next;
}
}
}
void FreeList(LinkList L)
{
LinkList p=L->next ;
while(p)
{
delete L;
L=p;
p=p->next;
}
}
滿足你所有的要求了
只是要自己去掉一些//
❺ 數據結構實驗 線性表中順序存儲結構的基本操作演算法(建順序表,查詢,插入,刪除,遍歷)
有序線性表插入一個數依然有序
#include<stdio.h>
#define MAXSIZE 6
typedef char datatype;
typedef struct SeqList
{
datatypedata[MAXSIZE];
int last;
}SeqList;
SeqList *init_SeqList()
{
SeqList *L;
L=(SeqList*)malloc(sizeof(SeqList));
L->last=-1;
return L;
}
int main()
{ SeqList *L;
int k,x,j;
intInser_SeqList(SeqList *L);
L->last=0;
L=init_SeqList();
for(k=0;k<(MAXSIZE-1);k++)
{ scanf("%d",&x);
L->data[k]=x;
L->last++;
}
Inser_SeqList(L);
for(j=0;j<L->last;j++)
printf("%d",L->data[j]);
return 0;
}
int Inser_SeqList(SeqList *L)
{
int j,x;
if(L->last==MAXSIZE-1)
{printf("表滿");
return (-1);
}
L->last++;
for(j=L->last;j>=0;j--)
if(L->data[j]<x)
L->data[L->last]=x;
else
L->data[j+1]=L->data[j];
return 1;
}
你好,上面是我的程序:符合你的1 2 4點 查詢、刪除操作在課本里找到 寫入即可 謝謝
❻ 數據結構實驗,線性表的順序存儲結構的實現
C++代碼編寫的
#include<iostream>
#include<string>
usingnamespacestd;
typedefintElemType;
typedefstructLNode
{
ElemTypedata;
structLNode*next;
}LinkList;
voidCreatListR(LinkList*&L,ElemTypea[],intn)
{
LinkList*s,*r;
inti;
//L=(LinkList*)malloc(sizeof(LinkList));
L=newLinkList();
r=L;
for(i=0;i<n;i++)
{
//s=(LinkList*)malloc(sizeof(LinkList));
s=newLinkList();
s->data=a[i];
r->next=s;
r=s;
}
r->next=NULL;
}
voidDispList(LinkList*L)
{
LinkList*p=L->next;
while(p!=NULL)
{
cout<<p->data<<"";
p=p->next;
}
cout<<endl;
}
intListInsert(LinkList*&L,inti,ElemTypee)
{
intj=0;
LinkList*p=L,*s;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
return0;
else
{
s=newLinkList();
s->data=e;
s->next=p->next;
p->next=s;
return1;
}
}
intListDelete(LinkList*&L,inti)
{
intj=0;
LinkList*p=L,*q;
while(j<i-1&&p!=NULL)
{
j++;
p=p->next;
}
if(p==NULL)
{
return0;
}
else
{
q=p->next;
if(q==NULL)
{
return0;
}
p->next=q->next;
inta=q->data;
free(q);
returna;
}
}
intmain()
{
LinkList*L,*L1;
inta[]={1,5,7,9,12,18,19,20,30,43,45,56,41,42,78};
intn=15,i,x,y,j;
CreatListR(L,a,n);
DispList(L);
cout<<"請輸入i"<<endl;
cin>>i;
cout<<"請輸入x"<<endl;
cin>>x;
ListInsert(L,i,x);
DispList(L);
cout<<"請輸入j"<<endl;
cin>>j;
y=ListDelete(L,j);
DispList(L);
cout<<y<<endl;
return0;
}
❼ 求數據結構試驗 線性表的順序存儲結構
#include<iostream.h>
#include<stdlib.h>
#include <malloc.h>
#define OVERFLOW 0
#define OK 1
#define ERROR 0
#define LIST_INIT_SIZE 100//線性表存儲空間的初始增量
#define LISTINCREMENT 10 // ?
typedef struct{
int * elem;// 存儲空間基址
int length;//當前長度
int listsize;//當前分配的存儲容量
}SqList;
SqList L;
int InitList_Sq(SqList & L){
//構造一個新的線性表。
L.elem=(int *)malloc(LIST_INIT_SIZE*sizeof(int));
if(!L.elem)exit(OVERFLOW);//存儲容量失敗
L.length=0; //空表長度為0
L.listsize=LIST_INIT_SIZE;//存儲初始容量
return OK;
}//InitList_Sq
int LIstInsert_Sq(SqList & L,int i,int e){
//在順序線性表L中第i位置之前插入新的元素e
if(i<1||i>L.length+1) return ERROR;
if(L.length>=L.listsize){
int * newbase=(int *)realloc(L.elem,(L.listsize+LISTINCREMENT)*sizeof(int));
if(!newbase)exit(OVERFLOW);
L.elem=newbase;
L.listsize+=LISTINCREMENT;
}
int * q=&(L.elem[i-1]);
for(int * p=&(L.elem[L.length-1]);p>=q;--p)*(p+1)=*p;
*q=e;
++L.length;
return OK;
}
int ListDelete_Sq(SqList&L,int i,int &e)
{
if((i<1)||(i>L.length))return ERROR;
int *p=&(L.elem[i-1]);
e=*p;
int *q=L.elem+L.length-1;
for(++p;p<=q;++p)*(p-1)=*p;
--L.length;
return OK;
}
void main()
{
SqList L;
int i,n;
int e;
cout<<"輸入順序表的個數:"<<endl;
cin>>n;
int *p=(int *)malloc(n*sizeof(int));
InitList_Sq(L);
cout<<"輸入線性表"<<n<<"個元素的值"<<endl;
for(i=0;i<n;i++)
{
cin>>p[i];
L.elem[i]=p[i];
}
cout<<endl;
L.length=i;
cout<<endl;
cout<<"輸入要插入元素的值"<<endl;
cin>>e;
cout<<endl;
cout<<"輸入要插入的位置"<<endl;
cin>>i;
LIstInsert_Sq( L, i, e);
for(i=0;i<n+1;i++)
cout<<L.elem[i];
cout<<endl;
cout<<"輸入要刪除的位置"<<endl;
cin>>i;
ListDelete_Sq(L,i,e)
;for(i=0;i<n;i++)
cout<<L.elem[i];
free(p);