单链表的逆向打印、删除无头的非尾节点、无头链表插入节点、约瑟环
//1、逆向打印链表(递归)voidPrintTailToHead(ListNode*pHead){ListNode*cur=pHead;if(cur!=NULL){PrintTailToHead(cur->_next);printf("%d",cur->_data);}}//2、删除无头链表中的非尾节点voidDelNoTail(ListNode*pos){assert(pos&&pos->_next);ListNode*next=pos->_next;swap(pos->_data,next->_data);pos->_next=next->_next;free(next);}//3、无头链表非头结点前插入一个节点voidInsertNoHead(ListNode*pos,DataTypex){ListNode*tmp=BuyNode(pos->_data);tmp->_next=pos->_next;pos->_next=tmp;pos->_data=x;}//4、约瑟夫环问题(假设是一个循环单链表)ListNode*Josephuscycle(ListNode*pHead,DataTypex){ListNode*cur=pHead;while(1){if(cur=cur->_next){returncur;}DataTypem=x;while(--x){cur=cur->_next;}ListNode*next=cur->_next;swap(cur->_data,next->_data);cur->_next=next->_next;free(next);}}
声明:本站所有文章资源内容,如无特殊说明或标注,均为采集网络资源。如若本站内容侵犯了原著者的合法权益,可联系本站删除。