插入法单链表的逆序代码

来源:互联网 发布:nginx安装lua模块 编辑:程序博客网 时间:2024/06/04 18:20
#include <iostream>#include <vector>#include <string>#include <strstream>#include <time.h>using namespace std;struct mynode{int data; mynode* next;};void  myreverse(mynode* phead){ mynode* h =((mynode*)phead)->next;mynode *p1=h; mynode *p2=h->next; mynode *p3=h->next->next;while(p2->next){//在头结点和成功逆序队列之间进行插入//cout<<"the while"<<p2->data<<"   "<<p3->data<<endl;p2->next=((mynode*)phead)->next;phead->next=p2;p1->next=p3;p2=p3;p3=p3->next;}p2->next=((mynode*)phead)->next;phead->next=p2;p1->next=p3;}void show(mynode* p){for(int i=0;i<10;i++){p=p->next;cout<<i<<" : "<<p->data<<endl;}}void create(mynode *phead){mynode* p;for(int i=0;i<10;i++){p=(mynode*)malloc(sizeof(mynode));p->data=rand()%100+1;p->next=(*phead).next;(*phead).next=p;}}int main (){mynode *phead;srand(time(0));phead=(mynode*)malloc(sizeof(mynode));(*phead).next=NULL;create(phead);show(phead);cout<<endl;cout<<"revers :"<<endl;myreverse(phead);cout<<"presult"<<endl;show(phead);return 0; }


//插入法进行单链表创建,并且插入法逆序单链表
	
				
		
原创粉丝点击