迴圈單鏈表

來源:互聯網
上載者:User
迴圈單鏈表
#include <stdio.h>#include <stdlib.h>#include <assert.h>#include <time.h>#define random(x) rand() % (x)typedef int Status;typedef int ElemType;typedef struct LNode{ElemType data;struct LNode* next;}*LNode;typedef struct LinkList{struct LNode* head;int size;}LinkList;int visit(void* data){printf("%d\t",*(int*)data);return 1;}Status InitLoopSingleList(LinkList* List);Status CreateLoopSingleList(LinkList* List,int n);void print(LinkList List,int visit());Status LoopLinkListReverse1(LinkList* List);Status BubbleSort(LinkList* List);Status Insert(LinkList* List,int i,ElemType e);Status Delete(LinkList* List,int i,ElemType* e);int main(){LinkList List;int n = 0;if(InitLoopSingleList(&List) == 0)exit(1);printf("please input the counter of number\n");scanf("%d",&n);CreateLoopSingleList(&List,n);print(List,&visit);LoopLinkListReverse1(&List);print(List,&visit);Insert(&List,4,0);print(List,&visit);BubbleSort(&List);print(List,&visit);return 0;}Status Delete(LinkList* List,int i,ElemType* e){assert(i >= 1 && i <= List -> size);LNode head = List -> head,p = head,q = NULL;int j = 1;while(p && j < i){p = p -> next;j++;}q = p -> next;*e = q -> data;p -> next = q -> next;free(q);List -> size--;return 1;}Status Insert(LinkList* List,int i,ElemType e){assert(i >= 1 && i <= List -> size + 1);LNode head = List -> head,p = head,pnew = NULL;int j = 1;while(p && j < i){p = p -> next;j++;}if((pnew = (LNode) malloc (sizeof(LNode))) == 0)return 0;pnew -> data = e;pnew -> next = p -> next;p -> next = pnew;List -> size++;return 1;}Status LoopLinkListReverse1(LinkList* List){assert(List -> head != NULL);if(List -> size == 1)return 1;LNode head = List -> head,p = head -> next -> next,q = NULL;head -> next -> next = head;while(p != head){q = p;p = p -> next;q -> next = head -> next;head -> next = q;}return 1;}Status BubbleSort(LinkList* List){assert(List -> head != NULL);if(List -> size < 2)return 1;LNode head = List -> head,p = NULL,q = NULL,ptail = head;while(head -> next != ptail){q = head;p = q -> next -> next;while(p != ptail){if(q -> next -> data > p -> data){q -> next -> next = p -> next;p -> next = q -> next;q -> next = p;}q = q -> next;p = q -> next -> next;}ptail = q -> next;}return 1;}void print(LinkList List,int visit()){LNode p = List.head -> next;while(p != List.head){visit(&p -> data);p = p -> next;}printf("\n");}Status CreateLoopSingleList(LinkList* List,int n){LNode p = List -> head,pnew = NULL;int i = 0;srand(time(NULL));while(i < n){if((pnew = (LNode) malloc (sizeof(struct LNode))) == 0)return 0;pnew -> data = random(100);pnew -> next = p -> next;p -> next = pnew;p = pnew;i++;}p -> next = List -> head;List -> size += n;return 1;}Status InitLoopSingleList(LinkList* List){LNode head;if((head = (LNode) malloc (sizeof(struct LNode))) == 0)return 0;head -> next = head;List -> head = head;List -> size = 0;return 1;}

 

聯繫我們

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