【資料結構】鏈式儲存單鏈表,資料結構鏈式單鏈

來源:互聯網
上載者:User

【資料結構】鏈式儲存單鏈表,資料結構鏈式單鏈


資料結構之單鏈表的鏈式儲存實現


//======================================================================  //  //        Copyright (C) 2014-2015 SCOTT      //        All rights reserved  //  //        filename: List.c//        description: a demo to display SeqList//  //        created by SCOTT at  01/28/2015 //        http://blog.csdn.net/scottly1//  //======================================================================  #include <stdio.h>#include <string.h>#include <malloc.h>#define TRUE1#define FALSE1typedef int Status;typedef int ElementType;typedef struct _tag_Node{ElementType data;struct _tag_Node *next;}NODE, *PNODE;PNODE initList(){int i, j = 0;int nLen;PNODE pHead = NULL;PNODE pNode = NULL;printf("Input The Length:");scanf("%d", &nLen);pHead = (PNODE)malloc(sizeof(NODE));pHead->data = nLen;                      // 頭結點資料域可用於儲存如鏈表長度等資訊。pHead->next = NULL;for(i=0; i<nLen; ++i, ++j){pNode = (PNODE)malloc(sizeof(NODE));printf("Input The %d val:", nLen-j);scanf("%d", &(pNode->data));pNode->next = pHead->next;           // 注意鏈表的產生技巧!pHead->next = pNode;}return pHead;}Status traverseList(PNODE list){int i = 1;PNODE p = list->next;ElementType data;while(p){data = p->data;printf("The %d Data is:%d\n", i++, data);p = p->next;}printf("The Length of List is %d\n\n", list->data);return TRUE;}Status deleteNode(PNODE list, int pos, PNODE ret){int i = 0;PNODE pHead = list;PNODE pTmp =  NULL;if(!pHead || pos <=0 || pos >pHead->data){return FALSE;}while(pHead->next && i<pos-1){pHead = pHead->next;i++;}if(!pHead || i>pos-1){return FALSE;}pTmp = pHead->next;pHead->next = pTmp->next;free(pTmp);--list->data;return TRUE;}Status insertNode(PNODE list, int pos, PNODE node){int i = 0;PNODE pHead = list;if(!pHead || pos <=0) //pos >pHead->data{return FALSE;}while(pHead->next && i<pos-1){pHead = pHead->next;++i;}node->next = pHead->next;pHead->next = node;++list->data;return TRUE;}int main(){PNODE list;PNODE ret;PNODE node;list = initList();traverseList(list);// 刪除測試deleteNode(list, 2, ret);traverseList(list);// 插入測試node = (PNODE)malloc(sizeof(NODE));node->data = 100;insertNode(list, 0, node);    // not successinsertNode(list, 1000, node); // insert in lasttraverseList(list);return 0;}

註:原創文章,轉載請註明出處:http://blog.csdn.net/scottly1/article/details/43247465


聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.