C Language Realization single-linked list, single-chain surface test interview

Source: Internet
Author: User

Single linked list is an important module of learning indispensable, in the interview will also be a lot of single-chain list variant problems, today they summarize the song to summarize the first is to achieve a simple single-linked list:

(Here, some simple implementation of the single-linked list operation function is not a note)

Typedef  int datatype;//typedef a type, and later if you want to change the type of data stored in a single-linked list node, you can change it directly here typedef struct  slistnode{datatype data;           //data struct  slistnode* next;  //refers to the pointer}slistnode of a downward node; Slistnode* buynode (DATATYPE X)//Create a single-linked list of nodes {slistnode* temp;    temp =   (slistnode*) malloc (sizeof (Slistnode));//For the node to open up a certain size of space temp->data = x;temp->next =  null;return temp;} Void pushback (slistnode* & phead, datatype x)//Insert data at the end of a single-linked list, to check two cases where the single-linked list is empty and not empty, process {//1. null//2. Not empty if  (phead == null) {phead = buynode (x);} else {slistnode* tail = phead;while  (tail->next != null) {Tail =  tail->next;} Tail->next = buynode (x);}} Void popback (slistnode* & phead)//delete the node at the end of the single-linked list {//1. Empty//2. A node//3. Multi-node if  (phead ==  null) {printf ("the&nbsp Slist is empty\n "); return;} else if  (phead->next == null) {free (phead);p head = null;} else{slistnode* tail = phead; slistnode* prev = null;while  (tail->next != null) {Prev = tail;tail  = tail->next;} Free (tail);p rev->next = null;}} Insert a node Void pushfront (slistnode* & phead, datatype x) {//1. Empty//2 in front of a single-linked list. Not Empty if  ( Phead == null) {phead = buynode (x);} else if  (phead != null) {slistnode* cur = buynode (x); cur->next =  phead;phead = cur;}} Delete a node Void popfront (slistnode* & phead) {if  (phead == null) {return;} in front of a single-linked list else if  (phead->next == null) {free (phead);p head = null;} else if  (phead != null) {slistnode* cur = phead;phead = phead-> Next;free (cur); cur =&nbsP NULL;}} Find the node and return it Slistnode* find (slistnode* phead, datatype x) {slistnode* cur =  phead;while  (cur) {if  (cur->data == x) {return cur;} Cur = cur->next;} Return null;}   //Inserts a node Void insert (slistnode* pos, datatype x) {assert (POS) at the specified location; Slistnode* temp = buynode (x);temp->next = pos->next;pos->next =  temp;}   //Delete the specified node Void erase (slistnode* phead,slistnode* pos) {assert (POS); assert (Phead);   (Phead == pos) {phead = phead->next;free (pos);p os = null;} slistnode* prev = phead;while  (prev) {if  (prev->next == pos) {prev-> Next = pos->next;free (POS);p os = null;break;} Prev = prev->next;}}   //single-linked list bubble sort, (only change the data of single-linked table nodes)   void bubble (slistnode* phead) {int exange =  0;if  (PHEAD&NBsp;== null | |  phead->next == null) {return;} slistnode* prev = phead; slistnode* cur = phead->next; slistnode* tail =null;
while  (tail != phead) {cur = phead->next;prev = phead;while  (cur  != tail) {            if  (prev-> Data > cur->data)//Compare node information, swap it if conditions are met {Datatype x;x = cur->data;cur->data  = prev->data;prev->data = x;} prev = cur;            cur =  cur->next;//refers to the next node, the implementation of the iteration}     tail = prev;//loop after the end of Prev point to the node, assigned to Tail}}   //Delete the non-tail node of a headless single-linked list, the idea is to exchange information about the node and the next node, deleting the node's latter node Void delnontailnode (slistnode* pos) { ASSERT (POS); assert (Pos->next); slistnode* del = pos->next;pos->data = del->data;pos->next =  Del->next;free (del);d El = null;}   //Inserts a node Void insertnontailnode (slistnode* pos,datatype x) {assert (POS) on a non-head node of the headless single-stranded list; SlistnOde* cur = buynode (Pos->data); slistnode* temp = pos->next;cur->next = temp;pos->next = cur;pos- >data = x;}   //find the middle node of a single linked list and can only traverse the list once (fast and slow pointer)   //quick pointer Walk Two steps full hands step, wait until the fast pointer to the tail, then the slow pointer just to the middle slistnode*  Findmidnode (Slistnode* phead) {assert (Phead);if  (phead->next == null) {return pHead;} else{slistnode* slow = phead; slistnode* fast = phead;  //here I give two kinds of wording, are correct          /*while  (FAST)//fast pointer is not empty, in the following condition to judge the next node of the fast pointer is also not empty {if  (fast->next) {fast = fast- >next->next;} Else{break;} Slow = slow->next;} */       while  (Fast && fast->next)// Note that the condition here is that the fast pointer and the next node of the fast pointer are not empty {slow = slow->next;fast = fast->next->next;}   return slow;//returns the slow node, which is the middle node}return null;}   //finds the penultimate K-node of the linked list, and onlyCan traverse the list once   //this is also the use of fast and slow hands, the quick pointer to go k step, the slow pointer to walk a step, the fast hand step, then the two pointer difference K step, when the fast pointer to empty, the slow pointer just point to the bottom K node Slistnode* findnode (slistnode* phead, int k) {assert (Phead); assert (k >= 0); slistnode* slow = phead; slistnode* fast = phead;//has a problem/*while  (Fast && fast->next) {while  (k--) {Fast = fast->next;} Fast = fast->next;slow = slow->next;} */while  (fast && --k) {fast = fast->next;if  (Fast == NULL) { Return null;}} while  (fast->next) {fast = fast->next;slow = slow->next;} Return slow;}   //print a single-linked list (using recursion) from the tail end Void printtailtohead (slistnode* phead) {if  (phead ==  NULL) {return;} Else{printtailtohead (Phead->next);p rintf ("%d ",  phead->data);}}   //Reverse Single-link list     It's important to create a new head node and remove each node of the single-linked list and insert it.
Before it, and then move it forward to form a new single-linked list that returns the head node of the new list Slistnode*  reverse (slistnode* phead) {slistnode* cur =  pHead; slistnode* newhead = null;while  (cur) {slistnode* temp = cur;cur =  cur->next;temp->next = newhead;newhead = temp;} Return newhead;}   //single-linked list implementation Joseph Ring//run-time first construction ring, note at the end of code wreath Slistnode* josephcycle (slistnode* phead, int m) { slistnode* cur = phead;while  (1) {if  (cur == null) {Return NULL;} else if  (Cur == cur->next)//Only one node left {return cur;} Else{int x = m;            while   (--x)//point to the node m and delete it, loop the program until only one node is left to return it {cur = cur->next;} slistnode* del = cur->next;cur->data = del->data;cur->next =  Del->next;            free (DEL);//InterpretationPlace the node and place the empty Del = null;}}}   //merges two ordered linked lists, which are still ordered after merging, picking a node, comparing the size to the tail of the new linked list, forming a new single-linked list, returning the head node of the new list slistnode* meragelist (slistnode* &NBSP;PHEAD1,&NBSP;SLISTNODE*&NBSP;PHEAD2) {if  (phead1 == null) {return phead2;} if  (phead2 == null) {return phead1;} Slistnode* newhead = phead1->data < phead2->data ? phead1:phead2 ;//while  (phead1 == null | |  phead2 == null)//{//if  (phead1->data < phead2->data)//{//newHead  = phead1;//phead1 = phead1->next;//}//else if  (phead1->data ==  Phead2->data)//{//newhead = phead1;//newhead->next = phead2;//phead1 =  phead1->next;//phead2 = phead2->next;//}//else//{//newhead = phead2;//phead2 =  phead2->next;//}//newhead = newhead->next;//newhead->next = null;//}//while   (Phead1)//{//newhead->next = phead1;//phead1 = phead1->next;//}//while  (pHead2) slistnode* newhead = null; slistnode* cur1 = phead1; slistnode* cur2 = phead2;if  (cur1->data < cur2->data) {newHead =  cur1;cur1 = cur1->next;} slistnode* tail = newhead;while  (CUR1&NBSP;&AMP;&AMP;&NBSP;CUR2) {if  (cur1->data  < cur2->data) {Tail->next = cur1;cur1 = cur1->next;} Else{tail->next = cur2;cur2 = cur2->next;} tail = tail->next; Tail->next = null;} if  (cur1 != null) {tail->next = cur1;} if  (cur2 != null) {tail->next = cur2;} Return newhead;}   /to determine whether the chain table with a ring (can be solved with a quick and slow pointer), the fast hand walk two steps, slow hands to take a step, to see if it will meet, met is band ring, do not want with the ring bool isslistcycle (slistnode*  Phead) {if  (phead == null) {printf ("The slist is
Empty\n "); return false;} slistnode* slow = phead; slistnode* fast = phead;while  (Fast && fast->next) {fast =  fast->next->next;slow = slow->next;if  (fast == slow) {SListNode* cur Length of  = slow;int length = 0;       //ring do{slow =  slow->next;length++;}  while  (Cur != slow);p rintf ("%d\n",  length);p rintf ("the slist have  Cycle\n "); return true;}} printf ("the slist no cycle\n"); return false;} The entry point of the Ring Slistnode* cycleentry (slistnode* phead) {if  (phead == null) {printf ("The  Slist is empty\n "); return null;} slistnode* slow = phead; slistnode* fast = phead;while  (Fast && fast->next) {fast =  fast->next->next;slow = slow->next;if  (fast == slow) {Return slow;}}} Whether the linked list intersects, if intersecting, the intersection (without a ring) points to the same node, not the same information node Slistnode* isitersect (slistnode* phead1,slistnode*  PHEAD2) {if  (phead1 == null) {printf ("The linked list does not intersect \ n"); return phead2;} if  (phead2 == null) {printf ("The linked list does not intersect \ n"); return phead1;} slistnode* cur1 = phead1; slistnode* cur2 = phead2;while  (CUR1&NBSP;&AMP;&AMP;&NBSP;CUR2) {if  (cur1 == &NBSP;CUR2) {printf ("linked list intersect, intersection:%d\n",  cur1->data); return cur1;} Cur1 = cur1->next;cur2 = cur2 ->next;} Return null;}


This article is from the "11423494" blog, please be sure to keep this source http://11433494.blog.51cto.com/11423494/1763656

C Language Realization single-linked list, single-chain surface test interview

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.