生活随笔
收集整理的這篇文章主要介紹了
实验报告: 线性表的基本操作及应用
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
實驗報告: 線性表的基本操作及應用
實驗內容
基本要求:
(1)實現單鏈表的創建;(2)實現單鏈表的插入;(3)實現單鏈表的刪除
(4)實現單鏈表的查找;(5)實現單鏈表的顯示;
#include<stdio.h>
#include<stdlib.h>
#define ERROR 0;
#define OK 1;typedef int ElemType
;
typedef int status
;typedef struct LNode
{ElemType data
;struct LNode
*next
;
}LNode
,*LinkList
;
status
CreatList_LA(LinkList
&LA
,int n
)
{LNode
*p
;int i
;LA
=(LinkList
)malloc(sizeof(LNode
));LA
->next
=NULL;for(i
=n
;i
>0;--i
){p
=(LinkList
)malloc(sizeof(LNode
));scanf("%d",&p
->data
);p
->next
=LA
->next
;LA
->next
=p
;}return OK
;
}
status
CreakList_LB(LinkList
&LB
,int n
)
{LNode
*p
;status i
;LB
=(LinkList
)malloc(sizeof(LNode
));LB
->next
=NULL;for(i
=n
;i
>0;--i
){p
=(LinkList
)malloc(sizeof(LNode
));scanf("%d",&p
->data
);p
->next
=LB
->next
;LB
->next
=p
;}return OK
;
}
status
ListInsert_LA(LinkList
&LA
,status i
,ElemType e
)
{LNode
*p
,*s
; status j
;p
=LA
;j
=0;while(p
&&j
<i
-1){p
=p
->next
;++j
; }if(!p
||j
>i
-1)return ERROR
;s
=(LinkList
)malloc(sizeof(LNode
));s
->data
=e
;s
->next
=p
->next
;p
->next
=s
;return OK
;
}
status
ListDelete_LA(LinkList
&LA
,int i
,ElemType
&e
)
{LNode
*p
,*q
;status j
;p
=LA
;j
=0;while(p
||j
<i
-1){p
=p
->next
;++j
;}if(p
->next
==NULL||j
>i
-1)return ERROR
;q
=p
->next
;p
->next
=q
->next
;e
=q
->data
; free(q
);return OK
;
}
status
GetElem_LA(LinkList LA
,int i
,ElemType e
)
{ LNode
*p
;p
=LA
;status j
;j
=0;while(p
&&j
<i
-1){p
=p
->next
;++j
;}if(!p
||j
>i
)return ERROR
;e
=p
->data
;return OK
;}
status
MergeList_L(LinkList
&LA
,LinkList
&LB
,LinkList
&LC
)
{LNode
*pa
,*pb
,*pc
;pa
=LA
->next
;pb
=LB
->next
;LC
=pc
=LA
;while(pa
&&pb
){if(pa
->data
<=pb
->data
) {pc
->next
=pa
;pc
=pa
;pa
->next
;}else{pc
->next
=pb
;pc
=pb
;pb
=pb
->next
;}pc
->next
=pa
?pa
:pb
;} free(LB
);}
status
printfList_L(LinkList
&L
)
{LNode
*p
;int i
;p
=L
->next
;while(p
){printf("%d ",p
->data
);p
=p
->next
;}
}int main()
{ElemType e
;LinkList LA
,LB
,LC
;status i
,n
;printf("輸入向所創建LA鏈表中插入幾個結點,n為:");scanf("%d",&n
); printf("輸入鏈表的結點數據:");CreatList_LA(LA
,n
) ;printf("輸入向所創建LB鏈表中插入幾個結點,n為:");scanf("%d",&n
); printf("輸入鏈表的結點數據:");CreakList_LB(LB
,n
); printf("輸出鏈表LA為:");printfList_L(LA
);printf("\n輸出鏈表LB為:");printfList_L(LB
);printf("\n輸入在鏈表第幾個位置插入結點,i,e為:");scanf("%d%d",&i
,&e
);ListInsert_LA(LA
,i
,e
);printf("\n插入后使出的鏈表為:");printfList_L(LA
);printf("\n輸入在鏈表第幾個位置刪除結點,i為:"); scanf("%d",&i
);ListDelete_LA(LA
,i
,e
);printf("\n刪除后的鏈表LA為:");printfList_L(LA
);printf("刪除的第%d個結點所儲存的元素為:%f",i
,e
);printf("\n輸入想查找第幾個元素結點,i為:");scanf("%d",&i
);GetElem_LA(LA
,i
,e
);printf("\n查找到的第i個元素為:");printf("%d",e
);printf("\n合并之后的鏈表為:");MergeList_L(LA
,LB
,LC
);printfList_L(LC
);return OK
;
}
【記得引用符&與變量名稱間有空格】
【定義的函數數據類型要與其中數據的數據類型一值】
總結
以上是生活随笔為你收集整理的实验报告: 线性表的基本操作及应用的全部內容,希望文章能夠幫你解決所遇到的問題。
如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。