zoukankan      html  css  js  c++  java
  • 链表的逆置

    单链表的逆置

    #include <iostream>
    #include <cstdlib>
    #include <cstdio>
    #include <ctime>
    using namespace std;
    
    typedef char ElemType;
    
    typedef struct Node{
    	ElemType data;
    	struct Node *next;
    }Node, *LinkList;
    
    LinkList CreateList()
    {
    	LinkList L;
    	ElemType c;
    	L = (LinkList)malloc(sizeof(Node));
    	L->next = NULL;
    	Node *p , *tail;
    	tail = L;
    	c = getchar();
    	while(c != '#')
    	{
    		p = (Node *)malloc(sizeof(Node));
    		p->data = c;
    		tail->next = p;
    		tail = p;
    		c = getchar();
    	}
    	tail->next = NULL;
    	return L;
    }
    
    void ShowList(LinkList L)
    {
    	Node *p;
    	p = L->next;
    	while(p != NULL)
    	{
    		cout << p->data << " ";
    		p = p->next;
    	}
    	cout << endl;
    }
    
    void ReverseList(LinkList L)
    {
    	Node *p, *q;
    	p = L->next;
    	L->next = NULL;
    	while(p != NULL)
    	{
    		q = p->next;
    		p->next = L->next;
    		L->next = p;
    		p = q;
    	}
    
    }
    
    int main()
    {
    	LinkList L;
    	L = CreateList();
    	ShowList(L);
    
    	ReverseList(L);
    	ShowList(L);
    	return 0;
    }
    


  • 相关阅读:
    模板
    2019牛客暑期多校训练营(第五场)
    Codeforces
    Codeforces
    SCUT
    模板
    Codeforces
    2019 Multi-University Training Contest 4
    Codeforces
    Codeforces
  • 原文地址:https://www.cnblogs.com/lgh1992314/p/5835157.html
Copyright © 2011-2022 走看看