鏈表的實現樣本

來源:互聯網
上載者:User

線性表的鏈式實現例子

#include <stdio.h>     #include <stdlib.h>               #include "ds.h"               #define ElemType int                 //資料元素的類型      Status print(ElemType e);            // 列印資料元素的方法     bool equal(ElemType a, ElemType b);  //比較兩個元素相等的方法                #include "linklist.h"               //列印鏈表內容      void PrintLinkList(LinkList L);               int main()     {         LinkList L;                       //1)初始化鏈表          InitList(L);                       //2)插入一些元素: 12,23,34,45         ListInsert(L,1,12);         ListInsert(L,1,23);         ListInsert(L,1,34);         ListInsert(L,1,45);                       //3)列印鏈表資訊          printf("\n開始時鏈表內容\n");         PrintLinkList(L);         printf("ListLength(L) : %d\n", ListLength(L));         printf("ListEmpty(L)  : %d\n", ListEmpty(L));                       //4)鏈表插入          printf("\n請輸入一個元素:");          ElemType x;         read(x);         printf("插入開頭:");         ListInsert(L,1,x);         PrintLinkList(L);         printf("插入末尾:");          ListInsert(L,ListLength(L)+1,x);         PrintLinkList(L);                       //5)鏈表刪除         printf("\n請選擇刪除第i(1..%d)個元素:", ListLength(L));         int i;         read(i);         ElemType e;          if ( ListDelete(L,i,e)==OK ) {             Status print(ElemType e);  //聲明print(e)函數                                printf("刪除"); print(e);             printf("成功\n");         } else        printf("刪除失敗.\n");         printf("鏈表內容:");         PrintLinkList(L);                       //6)元素定位         printf("\n請輸入一個元素以便定位:");         read(x);         i = LocateElem(L,x);         if ( i!=0 ) {             printf("該元素是表中的第%d個元素.\n",i);         } else        printf("該元素在表中不存在.\n");                        //7)清空鏈表         ClearList(L);         printf("\n最後鏈表內容\n");         PrintLinkList(L);         printf("ListLength(L) : %d\n", ListLength(L));         printf("ListEmpty(L)  : %d\n", ListEmpty(L));                        //8)銷毀鏈表          DestroyList(L);                       system("PAUSE");         return 0;     }               // 列印資料元素的方法      Status print(ElemType e)     {         printf("%5d",e);         return OK;     }               //比較兩個元素相等的方法     bool equal(ElemType a, ElemType b)     {         return a==b;     }               //列印鏈表內容      void PrintLinkList(LinkList L)     {         ListTraverse(L,print); //遍曆鏈表並print()每個元素          printf("\n");     }

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.