Single linked list (add, delete, find)

Source: Internet
Author: User

      constructs single-linked list with structure, realizes the basic function of single-linked list. Header file #pragma once#include<stdio.h> #include <assert.h> #include <malloc.h>typedef int  datatype;typedef struct listnode{datatype _data;struct listnode *_next;} listnode;//Initialize Void initlist (listnode **pphead) {*pphead = null;} Destroy node Void destorylist (listnode*& phead) {listnode* cur = phead;while  (cur) { Listnode* tmp = cur;cur = cur->_next;free (TMP);} Phead = null;} Listnode* buynode (datatype x) {listnode* tmp =  (listnode*) malloc (sizeof (ListNode)); ASSERT (TMP); tmp->_data = x;tmp->_next = null;return tmp;} Tail plug   void pushback (listnode** pphead, datatype x)    {assert ( Pphead);if  (*pphead = null) {*pphead = buynode (x);} else{listnode *tail = *pphead;while  (Tail->_neXt != null) {Tail = tail->_next;} Tail->_next = buynode (x);}} Head plug Void pushfront (listnode* &phead, datatype x) {if  (PHead == NULL) { Phead = buynode (x);} Else{listnode * tmp = buynode (x); tmp->_next = phead;phead = tmp;}} Void printlist (Listnode *phead) {listnode *cur = phead;while  (cur !=  NULL) {printf ("%d->",  cur->_data); cur = cur->_next;} printf ("null\n");} Tail Delete void popback (listnode* &phead) {if  (phead == null) {printf ("list is  Empty\n "); return;} else if  (phead->_next == null) {free (phead);p head = null;} else{listnode* prevtail = null, *tail = phead;while  (tail->_next !=  null) {Prevtail = tail;tail = tail->_next;} Prevtail->_next = null;free (tail);}} Head Delete VOID&NBSp Popfront (Listnode*& phead) {if  (phead == null) {printf ("list is empty.\n"); return;} Else{listnode* tmp = phead;phead = tmp->_next;free (TMP);}} Listnode *find (listnode *phead, datatype x) {Listnode *cur = phead;while   (cur) {if  (cur->_data == x) {return cur;} Cur = cur->_next;} Return null;} Void insert (listnode* pos, datatype x) {assert (POS); Listnode *tmp = buynode (x);tmp->_next = pos->_next;pos->_next =  tmp;/*listnode *next = pos->_next; Listnode *tmp = buynode (x);   //inserted node pos->_next = tmp;tmp->_next  = next;*/}void erase (Listnode* &phead, listnode *pos) {assert (POS);if  (pos  == phead) {phead = phead->_next;free (phead);} else{listnode* cur = phead;while  (cur) {if  (Cur->_next != pos) {cur->_next = pos->_next;free (POS); Cur = cur->_next;}}} Void delnontailnode (Listnode* pos)//delete the unknown header and not the tail node {assert (Pos&&pos->_next);p Os->_data  = pos->_next->_data; Listnode *next = pos->_next->_next;free (POS);p Os->_next = next;} Void remove (listnode* &phead, datatype x) {Listnode* ret = find (PHead, &NBSP;X);if  (ret) {Erase (Phead, ret);} /*listnode *node1 = phead; listnode *node2 = null;if  (phead == null) {return ;} else{if  (node1->_data == x) {phead = phead->_next;free (node1); return ;} else{while  (node1 != null) {node2 = node1;node2 = node2->_next;if  ( node2->_data== x) {node1->_next = node2->_next;free (node2); Node1 = node1->_next;}}} */}void reverse (listnode* &phead)       //reverse {if  (pHead ==  null | |  phead->_next == null) {return;} listnode* cur = phead; listnode* newhead = null;while  (cur) {listnode* tmp = cur;cur =  Cur->_next;tmp->_next = newhead;newhead = tmp;} Phead = newhead;} Void printtailtohead (Listnode *phead) {if  (phead) {printtailtohead (Phead->_next);p rintf ("%d\n" , phead->_data);}} Listnode* findmid (Listnode* phead)//Find the middle node of a single linked list, requires only one link list (fast and slow pointer, one step, the other two steps) {if  (phead ==  null | |  phead->_next == null) {return phead;} listnode* fast = phead; listnode* slow = phead;while  (FAST) {fast = fast->_next->_next;slow =  slow->_next;} Return slow;} Void insertfrontnode (listnode* pos,datatype x) {assert (POS); LIstnode* tmp = buynode (x); tmp->_next = pos->_next;pos->_next = tmp ;D atatype tmpdata = tmp->_data;tmp->_data = pos->_data;pos->_data =  tmpdata;} Listnode* josephcycle (listnode* phead, int m)//delete node {listnode* cur = phead; if  (Cur == null) return null;while  (1) {//Only one node left if  (Cur = cur->_next) {return cur;} int x = m;while  (--x)//Walk m-1 step {cur = cur->_next;} Replace method to delete cur->_data = cur->_next->_data; Listnode* del = cur->_next;cur->_next = del->_next;free (DEL);}} Quick Sort//bubble sort void sortlist (Listnode* phead) {if  (phead == null | |  phead->_next == null) return; listnode *tail = null; listnode*cur = phead->_next; listnode*prev = phead;while  (tail != phead) {INT&NBsp;exchange = 0;//optimization does not exchange the situation while  (Cur != tail) {if  (cur->_data <prev- >_data) {Datatype temp = cur->_data;cur->_data = prev->_data;prev->_ data = temp;exchange = 1;} Prev = cur;cur = cur->_next;} if  (exchange == 0) return;tail = prev;prev = phead;cur = phead- >_next;}} Merge two ordered linked lists, still ordered     first pick    rear listnode* mergelist (listnode *phead1,  LISTNODE*&NBSP;PHEAD2) {if  (phead1 == null) {return phead2;} if  (phead2 == null) {return phead1;} Listnode*cur1 = phead1;} Listnode* findktailnode (listnode* phead, int k) {listnode *fast = phead,  *slow = pHead;while  (--k) {if  (fast == null) {return null;} fast = fast->_next;if  (fast == null) {return null;}} While  (fast->_next) {slow = slow->_next;fast = fast->_next;} Return slow;} Merge two ordered linked list listnode* mergelist (listnode*phead1, listnode*phead2) {if  (phead1 == null) return phead2;if  (phead2 == null) return phead1; Listnode*newhead; listnode*cur1 = phead1; listnode*cur2 = phead2; listnode*tail = null;if  (cur1->_data < cur2->_data) {NewHead = cur1 ; cur1 = cur1->_next;} Else{newhead = cur2;cur2 = cur2->_next;} tail = newhead;//while  (CUR1&AMP;&AMP;CUR2) {if  (cur1->_data< cur2->_data) { Tail->_next = cur1;cur1 = cur1->_next;} Else{tail->_next = cur2;cur2 = cur2->_next;} tail = tail->_next;if  (cur1 == null) tail->_next = cur2;if  (cur2  == null) Tail->_next = cur1;} Return newhead;} SentencedBroken chain list with ring, ring length, ring entry point void ringlist (Listnode* phead) {listnode*slow = phead; listnode*fast = phead;while  (Fast->_next) {slow = slow->_next;fast =  fast->_next;if  (Fast->_next) fast = fast->_next;if  (slow == fast) {printf (  "Ring in list \ n"  );//listnode *tmp = slow;slow = slow->_next;int k  = 1;while  (slow != fast) {slow = slow->_next;k++;} printf ("Chain ring length of  %d\n", k );//One step will inevitably meet in the ring where the last encounter//when the slow into the ring, will inevitably meet with fast, initially want to encounter is the ring entry point slow =  pHead;while  (slow != fast) {slow = slow->_next;fast = fast->_ Next;} printf (  "Ring entry value:%d\n"  , slow->_data ); return;}} printf ("No ring in the list \ n");} Linked list intersect void intersect (listnode*phead1, listnode * phead2) {if  (PHead1 == NULL  | |  phead2 == null) {printf (  list is not  insersect\n " ); return;} listnode *cur1 = phead1; listnode *cur2;while  (CUR1) {cur2 = phead2;cur1 = cur1->_next;while  (cur2 ) {if  (CUR1&NBSP;==&NBSP;CUR2) {printf (  "linked list intersection, intersection value:%d\n", cur1->_data ); return;} Cur2 = cur2->_next;}} printf (  "linked list does not intersect \ n"); return;} Int size (Listnode*phead) {assert (Phead);int size = 0; listnode*cur = phead->_next;while  (cur) {++size;cur = cur->_next;} Return size;}



Test



#include <stdio.h>

#include "ListNode.h"


void Test1 ()

{

ListNode * list = NULL;

Pushback (&list, 1);

Pushback (&list, 2);

Printlist (list);


Popfront (list);

Popfront (list);

Popfront (list);

Printlist (list);



listnode* ret = Find (list, 3);

Erase (list, ret);

Printlist (list);


ListNode *ret1 = Find (list, 3);

Insertfrontnode (list, ret1->_data);

}


int main ()

{

Test1 ();

return 0;

}


This article is from the "two negative" blog, please be sure to keep this source http://10324470.blog.51cto.com/10314470/1770126

Single linked list (add, delete, find)

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.