資料結構基礎(1)–>雙向鏈表

來源:互聯網
上載者:User

linklist.h

#ifndefLINK_LIST_H#defineLINK_LIST_H1#include <stdio.h>#include <string.h>#include <malloc.h>struct stu{charname[32];charstu_num[32];charsex[16];intage;structstu*next;structstu*pre;};struct stu *initlist(struct stu **head);void destroylist(struct stu *head);void clearlist(struct stu *head);int listempty(const struct stu *head);int listlength(const struct stu *head);struct stu *getelem(const struct stu *head, int i);void listinsert(struct stu *head, struct stu *new);void listdelete(struct stu *node);#endif /* LINK_LIST_H */

linklist.c

#include <linklist.h>struct stu *initlist(struct stu **head){*head = (struct stu *)malloc(sizeof(struct stu));if(!*head) {return NULL;}memset(*head, 0, sizeof(struct stu));(*head)->next = *head;(*head)->pre = *head;return *head;}void destroylist(struct stu *head){clearlist(head);free(head);return;}void clearlist(struct stu *head){struct stu *n;for(n=head->next; head->next!=head; n=head->next) {head->next = n->next;n->next->pre = head;free(n);}return;}int listempty(const struct stu *head){return head->next == head;}int listlength(const struct stu *head){int i = 0;struct stu *n = head->next;while(n != head) {n = n->next;++i;}return i;}struct stu *getelem(const struct stu *head, int i){struct stu *n = (struct stu *)head;for(; i--&&((n=n->next)!=head);)/* nothing*/;return n==head ? NULL:n;}void listinsert(struct stu *head, struct stu *new){new->next = head;new->pre = head->pre;head->pre->next = new;head->pre = new;return;}void listdelete(struct stu *node){node->pre->next = node->next;node->next->pre = node->pre;free(node);return;}

聯繫我們

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