【数据结构复习】链表的倒置(头插法倒置)

#include <bits/stdc++.h>
using namespace std;
typedef int ElemType;
struct LNode{
    ElemType data;
    LNode *next;
};
LNode *head,*tail;

void init(){
    head = (LNode*)malloc(sizeof(LNode));
    head->next = NULL;
    tail = head;
}

void input_data(){
    int x;
    cin >> x;
    while (x!=-1){
        LNode *temp = (LNode*)malloc(sizeof(LNode));
        temp->data = x;
        temp->next = NULL;
        tail->next = temp;
        tail = temp;
        cin >> x;
    }
}

//关键函数
void _reverse(){
    LNode *p = head->next;
    head->next = NULL;
    while (p){
        LNode *temp = p->next;//记录下当前遍历到的这个节点的下一个
        p->next = head->next;//这个几点的下一个节点接在头结点后面的那个节点
        head->next = p;//头结点的后一个节点指向该节点,从而完成插入过程
        p = temp;
    }
}

void print(){
    LNode *temp = head->next;
    while (temp){
        cout<<temp->data<<" ";
        temp = temp->next;
    }
}

int main(){
    init();
    //freopen("D://rush.txt","r",stdin);
    input_data();
    _reverse();
    print();
    fclose(stdin);
    return 0;
}

  

原文地址:https://www.cnblogs.com/AWCXV/p/11568644.html

时间: 2024-07-31 21:51:44

【数据结构复习】链表的倒置(头插法倒置)的相关文章

单链表 初始化 创建 头插法 尾插法 插入 删除 查找 合并 长度

#include <stdio.h> #include <stdlib.h> #define OK 1 #define ERROR -1 #define TRUE 1 #define FALSE -1 #define NULL 0 #define OVERFLOW -2 #define ElemType int #define Status int typedef int ElemType typedef int Status #define LEN sizeof(LNode) #

单链表的练习-头插法

/*单链表的练习-头插法*/ /*单链表由头结点就可以唯一确定*/ #include <malloc.h> #include <stdio.h> #include <stdlib.h> //定义单链表结构 typedef struct Node{ int data;  //数据域 Node * pNext;  //指针域 }NODE,* PNODE; PNODE create_list(void);  //创建链表 void show_list(PNODE pHead)

数据结构之链表---单链表的实现

public class Link { /** * 链结点 */ private int iData; private double dData; public Link next; public Link(int iData,double dData) { this.dData = dData; this.iData = iData; } //打印节点 public void displayLink() { System.out.println("{"+iData+",&q

大话数据结构——线性表-链式存储之头插法创建链表

1 #include<iostream> 2 3 #include<time.h> 4 #include <stdlib.h> 5 6 using namespace std; 7 8 #define OK 1 9 #define TRUE 1 10 #define FALSE 0 11 #define ERROR 0 12 13 typedef int status;//返回的状态值 14 typedef int elemtype;//节点里数据的类型 15 16 /

数据结构—头插法逆转单链表——空间复杂度为O(1)

#if 1 #include<stdio.h> #include<stdlib.h> #include<iostream> using namespace std; struct Node { int data; Node *next; }; //初始化 Node *init() { Node *head=new Node; head->next=NULL; return head; } //头插法创建节点 void insetList(Node *head,in

博客第二天——头插法建立单链表

今天是第二天,今天遇到一个题:本题要求实现一个函数,求单链表L结点的阶乘和.这里默认所有结点的值非负,且题目保证结果在int范围内. 首先考虑这个题目本身不难,鉴于本人刚学习数据结构,题目中已给代码的单链表的创建值得我考虑.题目中给的是头插法代码如下:分析一下这段代码 int N, i; List L, p; scanf("%d", &N); L = NULL; for ( i=0; i<N; i++ ) { p = (List)malloc(sizeof(struct

建立链表—头插法、尾插法—有无头结点

1.建立链表-头插法-头结点 1 //建立链表-头插法-头结点 2 LinkList CreatList_head() 3 { 4 DataType x; //数据 5 LinkList p,head; //结点 6 head = (LinkList)malloc(sizeof(LNode)); 7 head->next = NULL; 8 head->data = 0; 9 10 scanf("%d",&x); 11 while(x != 999) 12 { 13

复习(数据结构):链表:c语言:练习

库函数 #include "stdio.h" #include "string.h" #include "ctype.h" #include "stdlib.h" #include "io.h" #include "math.h" #include "time.h" #define OK 1 #define ERROR 0 #define TRUE 1 #define

《线性表的基础操作(实现了可以选择在创建初始链表时,是用头插法,还是用尾插法)》

#include<stdio.h>#include<stdlib.h>#include<conio.h>#define TRUE 1#define FALSE 0#define OK 1#define ERROR 0#define INFEASIBLE -1#define OVERFLOW -2#define NULL 0typedef int Status;typedef int ElemType; //------线性表的单链表存储结构-------- typede