Single-cycle linked list (represented by the tail pointer) and the tail pointer

Source: Internet
Author: User

Single-cycle linked list (represented by the tail pointer) and the tail pointer

1 # include <stdio. h> 2 # include <stdlib. h> 3/* single-cycle linked list represented by the tail pointer, compared to the single-cycle linked list represented by the header pointer: 4 * when the latter is searching for the first node of the linked list, the time complexity is O (1). When searching for the last element of the chain 5 * Table, the time complexity is O (n ). 6 * when searching for the first node of the linked list, the event complexity is O (1) (tail-> next 7 *-> next-> data is the value of the first element). When looking for the last node, the time 8 * complexity is O (1 ). 9 **/10 typedef struct Node {11 int data; 12 struct Node * next; 13} Node; 14 typedef struct Node * LinkList; 15 16 void Initial_SCL_tail (LinkList * L) {17 * L = (LinkList) malloc (sizeof (struct Node); 18 if (! (* L) {19 printf ("Error: Initial: malloc! \ N "); 20 exit (1); 21} 22 (* L)-> next = * L; 23} 24 void Create_SCLtail (LinkList * L, int number) {25 int I; 26 LinkList new; 27 LinkList tail = * L; 28 printf ("--- Create LinkList --- \ n"); 29 for (I = 1; I <= number; I ++) {30 new = (LinkList) malloc (sizeof (struct Node); 31 if (! New) {32 printf ("Error: Create_SCLtail: malloc: % d \ n", I); 33 exit (1); 34} 35 printf ("please enter % d element: ", I); 36 scanf (" % d ", & (new-> data); 37 tail-> next = new; 38 new-> next = (* L); 39 tail = new; 40} 41 * L = new; 42} 43 void connect (LinkList LA, LinkList * LB) {44/* link LB to the last 45 of LA **/46 LinkList temp, temp1; 47 temp = LA-> next; 48 temp1 = (* LB)-> next; 49 LA-> next = (* LB)-> next; 50 (* LB)-> next = temp; 51 fr Ee (temp1); 52} 53 void Display_SCLtail (LinkList L) {54 LinkList temp; 55 printf ("Element of List is :"); 56 temp = L-> next; // temp now point to first node; 57 while (temp! = L-> next) {58 printf ("% d", temp-> data); 59 temp = temp-> next; 60} 61 printf ("\ n "); 62} 63 int main () {64 LinkList L1, L2; 65 Create_SCLtail (& L1, 5); 66 Create_SCLtail (& L2, 8); 67 Display_SCLtail (L1 ); 68 Display_SCLtail (L2); 69 connect (L1, & L2); 70 Display_SCLtail (L2); 71 return 0; 72}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.