⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 single_chain_reverse.cpp

📁 单链表逆置,数据结构作业............
💻 CPP
字号:
#include <iostream>

using namespace std;

typedef struct node
{
    struct node* next;
    int data;
}node,*link_list;

void create_list(link_list &L,int n)
{
    L=NULL;
    cout<<"please input "<<n<<" numbers:"<<endl;
    for(int i=1;i<=n;i++){
		node* p=new node;
        cin>>p->data;
		//insert to head
		p->next=L;
        L=p;
    }
    cout<<"============"<<endl;//separator
}

void print_list(link_list L)
{
    node* temp=L;
    while(temp){
		cout<<temp->data<<" ";
        temp=temp->next;
    }    
    cout<<endl;
}

void reverse_list(link_list &p)
{
    node	*pre=NULL,
			*cur=p,			
			*next=cur->next;
    
    while(next){
        cur->next=pre;
        pre=cur;
        cur=next;
        next=cur->next;
    }    
    p=cur;
    cur->next=pre;
}

void del_list(link_list &p)
{
    node	*cur=p,
			*next=p->next;
    
    while(next){
        delete cur;
        cur=next;
        next=cur->next;
    }    
    delete cur;
    cout<<"the chain has been successfully deleted"<<endl;
}

int main()
{
    link_list L;
    int len;
    cout<<"the length of the list?"<<endl;
    cin>>len;
    
    create_list(L,len);
    print_list(L);
    reverse_list(L);
	cout<<"reversed order:"<<endl;
    print_list(L);
    del_list(L);
    return 0;
}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -