當前位置:首頁 » 存儲配置 » 線性表的鏈式存儲實現

線性表的鏈式存儲實現

發布時間: 2022-12-19 18:51:24

㈠ 1、線性表順序存儲方式操作2、線性表鏈式存儲方法操作:。兩個都用c語言

下面是從我的博客裡面給你找的,你參考一下。更多的數據結構的內容,也可以去看我的博客。

/************************************************************
說明:
1、主函數內的代碼是為了測試方便,可以自行修改。
2、宏定義NEWS是人機交互信息提示,若不需要,可修改為0。
3、若是windows系統,請將258行中的clear修改為cls。
4、在輸入數據後,請多按一下回車,實現清屏。
環境:ubuntu12.04LTS、codeblocks10.05、2014-04-02
************************************************************/
#include<iostream>
#include<stdlib.h>
#include<malloc.h>
#include<cstring>
#defineNEWS1
#defineLIST_INIT_SIZE100
#defineLIST_ADD_SIZE10
#defineNEW_SIZE(L->list_size+LIST_ADD_SIZE)
usingnamespacestd;
typedefintElem_Type;
typedefstruct
{
Elem_Type*data;//元素
intlen;//長度
intlist_size;//空間
}Sq_List;
typedefenum
{
OK=0,//正常
ERROR=-1,//邏輯異常
OVER=-2//內存異常
}Status;
/******************************
函數名:Init_List
功能:構造、初始化線性表
******************************/
Sq_List*Init_List(void)
{
Sq_List*L=(Sq_List*)malloc(sizeof(Sq_List));
if(!L)
exit(OVER);
L->data=(Elem_Type*)malloc(LIST_INIT_SIZE*sizeof(Elem_Type));
if(!L->data)
exit(OVER);
L->len=0;
L->list_size=LIST_INIT_SIZE;
#if(NEWS)
cout<<"構造成功,已初始化"<<endl;
#endif
returnL;
}
/******************************
函數名:Destroy_List
功能:銷毀線性表
******************************/
StatusDestroy_List(Sq_List*L)
{
free(L);
#if(NEWS)
cout<<"銷毀成功,請不要再使用"<<endl;
#endif
returnOK;
}
/******************************
函數名:Clear_List
功能:清空線性表
******************************/
StatusClear_List(Sq_List*L)
{
memset(L->data,-1,sizeof(L->data));
L->len=0;
#if(NEWS)
cout<<"已全部清空"<<endl;
#endif
returnOK;
}
/******************************
函數名:Empty_List
功能:判斷線性表是否為空
******************************/
boolEmpty_List(Sq_List*L)
{
returnL->len==0;
}
/******************************
函數名:Length_List
功能:獲取線性表長度
******************************/
intLength_List(Sq_List*L)
{
returnL->len;
}
/******************************
函數名:Get_Elem_List
功能:指定位置獲取元素
******************************/
StatusGet_Elem_List(Sq_List*L,intindex,Elem_Type*e)
{
if(!(1<=index&&index<=Length_List(L)))
#if(NEWS)
{
cout<<"獲取位置不合法"<<endl
<<"下面顯示的數據是上次輸入的num的值,請注意!!!"<<endl;
returnERROR;
}
#else
returnERROR;
#endif
*e=L->data[index-1];
returnOK;
}
/******************************
函數名:Insert_List
功能:指定位置插入元素
******************************/
StatusInsert_List(Sq_List*L,intindex,Elem_Typee)
{
if(!(1<=index&&index<=Length_List(L)+1))
#if(NEWS)
{
cout<<"插入位置不合法"<<endl;
returnERROR;
}
#else
returnERROR;
#endif
if(Length_List(L)>=L->list_size)
#if(NEWS)
cout<<"剛才增加了存儲空間"<<endl;
#endif
L->data=(Elem_Type*)realloc(L->data,NEW_SIZE*sizeof(Elem_Type));
if(!L->data)
exit(OVER);

L->list_size+=LIST_ADD_SIZE;

for(inti=Length_List(L);i>=index-1;i--)
L->data[i+1]=L->data[i];

L->data[index-1]=e;
L->len++;
#if(NEWS)
cout<<"插入成功"<<endl;
#endif
returnOK;
}
/******************************
函數名:Delete_List
功能:指定位置刪除元素
******************************/
StatusDelete_List(Sq_List*L,intindex,Elem_Type*e)
{
if(Empty_List(L)||!(1<=index&&index<=Length_List(L)))
#if(NEWS)
{
cout<<"刪除位置不合法or目前是空表"<<endl;
returnERROR;
}
#else
returnERROR;
#endif
*e=L->data[index-1];
for(inti=index;i<Length_List(L);i++)
L->data[i-1]=L->data[i];
#if(NEWS)
cout<<"刪除成功"<<endl;
#endif
L->len--;
returnOK;
}
/******************************
函數名:Print_List
功能:輸出所有元素
******************************/
StatusPrint_List(Sq_List*L)
{
if(Empty_List(L))
returnERROR;
inttemp;
for(inti=1;i<=Length_List(L);i++)
{
Get_Elem_List(L,i,&temp);
cout<<temp<<"";
}
cout<<endl;
returnOK;
}
/******************************
函數名:print_news
功能:方便用戶選擇
******************************/
voidprint_news(void)
{
cout<<" ********************"
<<"*****************************"<<endl
<<" * 0建立、初始化 *"<<endl
<<" * *"<<endl
<<" * 1插入元素 *"<<endl
<<" * *"<<endl
<<" * 2刪除元素 *"<<endl
<<" * *"<<endl
<<" * 3銷毀 *"<<endl
<<" * *"<<endl
<<" * 4獲取表長 *"<<endl
<<" * *"<<endl
<<" * 5清空 *"<<endl
<<" * *"<<endl
<<" * 6獲取元素 *"<<endl
<<" * *"<<endl
<<" * 7列印 *"<<endl
<<" * *"<<endl
<<" * 8退出程序 *"<<endl
<<" ********************"
<<"*****************************"<<endl;
}
intmain(void)
{
Sq_List*test=NULL;
while(true)
{
print_news();
intchoose,index,num;
cout<<"要進行什麼操作?"<<endl;
cin>>choose;
switch(choose)
{
case0:
test=Init_List();break;
case1:
cout<<"插入位置"<<endl;
cin>>index;
cout<<"插入數據"<<endl;
cin>>num;
Insert_List(test,index,num);break;
case2:
cout<<"刪除的位置"<<endl;
cin>>index;
Delete_List(test,index,&num);
cout<<"被刪除的元素是:"<<num<<endl;break;
case3:
Destroy_List(test);break;
case4:
cout<<Length_List(test)<<endl;break;
case5:
Clear_List(test);break;
case6:
cout<<"獲取哪個位置的元素"<<endl;
cin>>index;
Get_Elem_List(test,index,&num);
cout<<num<<endl;break;
case7:
Print_List(test);break;
case8:
return0;
default:
break;
}
getchar();
getchar();
system("clear");
}
return0;
}

㈡ 線性表兩種 存儲結構各自的優缺點有哪些

線性表的鏈式存儲結構:

優點:

插入和刪除不需要移動插入時只需要對插入位置後的一個元素進行操作,不需要大量的移動元素。空間有效利用高。

缺點:

大量訪問操作時不如順序存儲結構,因為每次都需要從頭開始遍歷整個線性表直到找到相應的元素為止。

線性表的順序存儲結構:

優點:

可隨機存取表中任一元素。因為有下標可以操作可以快速的定位到指定位置的元素,但是不知道位置的話也需要順序遍歷。

缺點:

插入或刪除操作時,需大量移動元素。合適在很少進行插入和刪除運算的情況下。

(2)線性表的鏈式存儲實現擴展閱讀:

線性表的特徵

集合中必存在唯一的一個「第一元素」。

集合中必存在唯一的一個 「最後元素」 。

除最後一個元素之外,均有唯一的後繼(後件)。

除第一個元素之外,均有唯一的前驅(前件)。

線性表的基本操作

MakeEmpty(L) 這是一個將L變為空表的方法。

Length(L) 返回表L的長度,即表中元素個數。

Get(L,i) 這是一個函數,函數值為L中位置i處的元素(1≤i≤n)。

Prior(L,i) 取i的前驅元素。

Next(L,i) 取i的後繼元素。

Locate(L,x) 這是一個函數,函數值為元素x在L中的位置。

Insert(L,i,x)在表L的位置i處插入元素x,將原占據位置i的元素及後面的元素都向後推一個位置。

Delete(L,p) 從表L中刪除位置p處的元素。

IsEmpty(L) 如果表L為空表(長度為0)則返回true,否則返回false。

Clear(L)清除所有元素。

Init(L)同第一個,初始化線性表為空。

Traverse(L)遍歷輸出所有元素。

Find(L,x)查找並返回元素。

Update(L,x)修改元素。

Sort(L)對所有元素重新按給定的條件排序。

strstr(string1,string2)用於字元數組的求string1中出現string2的首地址。

參考資料來源:網路-線性表

㈢ 線性表的鏈式存儲結構

結點由存放數據元素的數據域和存放後繼結點地址的指針域組成。

n個結點鏈成一個鏈表,即為線性表的鏈式存儲結構。

在單鏈表的第一個結點前附設一個頭結點,頭結點的數據域可以不存儲任何數據,頭結點的指針域存儲指向第一個結點的指針,鏈表可以沒有頭結點。

頭指針是鏈表指向第一個結點的指針,如果鏈表有頭結點,頭指針指向頭結點。

獲得鏈表第i個數據的方法,定義一個指針,從鏈表第一個數據開始遍歷,不斷指向下一個結點,直到第i個。

㈣ 線性表的鏈式存儲結構是一種()存儲結構

線性表的鏈式存儲結構是一種順序存儲的存儲結構。

線性表的鏈式存儲結構中的每一個存儲結點不僅含有一個數據元素,還包括指針,每一個指針指向一個與本結點有邏輯關系的結點,此類存儲方式屬於順序存儲;線性表是最基本、最簡單、也是最常用的一種數據結構。線性表(linear list)是數據結構的一種,一個線性表是n個具有相同特性的數據元素的有限序列。

(4)線性表的鏈式存儲實現擴展閱讀:

線性表中數據元素之間的關系是一對一的關系,即除了第一個和最後一個數據元素之外,其它數據元素都是首尾相接的(注意,這句話只適用大部分線性表,而不是全部。

比如,循環鏈表邏輯層次上也是一種線性表(存儲層次上屬於鏈式存儲,但是把最後一個數據元素的尾指針指向了首位結點)。

㈤ 線性表鏈式存儲結構的基本操作演算法實現

這是我學數據結構時親手碼的,好用的話記得給分。。。。。
#include<iostream>
using namespace std;

//線性表的單鏈表存儲表示
struct LNode
{
int data;
struct LNode *next;
};

//逆序創建鏈表
void CreateList(LNode *L,int a[],int n)
{
LNode *s;
L->next = NULL;
for(int i=n-1;i>=0;--i)
{
s= new LNode;
s->data=a[i];
s->next=L->next;
L->next=s;
}
}
//顯示鏈表
void display(LNode *L)
{
LNode *p;
p=L->next;
while(p)
{
cout<<p->data<<" ";
p=p->next;
}
}
//插入結點操作
void ListInsert(LNode *L,int d, LNode *s)
{
LNode *p;
int i=1;
p=L->next;
while(i<d-1)
{
p=p->next;
i++;
}
s->next=p->next;
p->next=s;
}
//刪除節點操作
void ListDelete(LNode *L,int d,int &e)
{
LNode *p,*q;
int i=0;
p=L->next ;
while(i<d-1)
{
q=p;
p=p->next;
i++;
}
e=p->data;
q->next=p->next;
delete p;
}
//查找元素
LNode* LocalElem(LNode *L,int e)
{
LNode *p;
p=L->next;
while(p&&p->data!=e) p=p->next;
return p;
}
//逆置單鏈表
void InvertLinkedList(LNode *L)
{
LNode *p,*s;
p=L->next;
L->next=NULL;
while(p)
{
s=p;
p=p->next;
s->next=L->next;
L->next=s;
}
}

int main()
{
LNode *H;
int n;
cout<<"please enter the length of the list: ";
cin>>n;
int *A=new int[n];
int i;
H=new LNode;
H->next=NULL;
cout<<"please enter "<<n<<" number of the list:"<<endl;
for(i=0;i<n;i++)
cin>>A[i];
CreateList(H,A,n);
cout<<"show the members of the list: ";
display(H);
cout<<endl;

int d;
cout<<"please enter the address of the add member: ";
cin>>d;
LNode *s;
s= new LNode;//初始化局部變數s
cout<<"please enter the data of the add member: ";
cin>>s->data;
ListInsert(H,d, s);
display(H);
cout<<endl;

int p;
cout<<"please enter the address of the delete member: ";
cin>>p;
int c=0;
int &e=c;//非常量引用的初始值必須為左值!!!
ListDelete(H,p,e);
display(H);
cout<<endl;

int g;
cout<<"please enter the member that you want to find: ";
cin>>g;
cout<<"the local of the member is: "<<LocalElem(H,g);
cout<<endl;

InvertLinkedList(H);
cout<<"the invertlinklist is: ";
display(H);
cout<<endl;
return 0;
}

花時間好好看看,一定要看懂

熱點內容
java返回this 發布:2025-10-20 08:28:16 瀏覽:600
製作腳本網站 發布:2025-10-20 08:17:34 瀏覽:892
python中的init方法 發布:2025-10-20 08:17:33 瀏覽:586
圖案密碼什麼意思 發布:2025-10-20 08:16:56 瀏覽:769
怎麼清理微信視頻緩存 發布:2025-10-20 08:12:37 瀏覽:690
c語言編譯器怎麼看執行過程 發布:2025-10-20 08:00:32 瀏覽:1016
郵箱如何填寫發信伺服器 發布:2025-10-20 07:45:27 瀏覽:261
shell腳本入門案例 發布:2025-10-20 07:44:45 瀏覽:119
怎麼上傳照片瀏覽上傳 發布:2025-10-20 07:44:03 瀏覽:809
python股票數據獲取 發布:2025-10-20 07:39:44 瀏覽:718