当前位置:网站首页>[c language] use the reverse order output of the linked list (bidirectional linked list)
[c language] use the reverse order output of the linked list (bidirectional linked list)
2022-07-29 04:30:00 【LastWhisperw】
Topic content :
Your program will read in a series of positive integers , I don't know the number of positive integers in advance , Once you read it -1, It means the end of input . then , Output the number read in the reverse order of the input , Not including the end of the last logo -1.
Input format :
A series of positive integers , Input -1 End of the said ,-1 Not part of the input data .
Output format :
Output all integers in the reverse order of input , Each integer is followed by a space to distinguish it from the following integer , There is also a space after the last integer .
sample input :
1 2 3 4 -1
sample output :
4 3 2 1
The time limit :500ms Memory limit :32000kb
Ideas : Use Double linked list , Record the location of the last node , Then visit the precursor nodes one by one and output . I wrote a lot of things I didn't understand, but the result seemed to be right , People are numb .
Need to define include Pointer to the previous node former And a pointer to the next node next The node of
#include <stdlib.h>
#include<stdio.h>
typedef struct NODE{
int data;
struct NODE * next;
struct NODE * former;
}numset; // Each node of the linked list , Including data , Previous node pointer , Next node pointer
typedef struct List{
numset* head;
numset* end;
numset* now;
int size;
}list; // To facilitate the transfer of linked lists into functions , The structure of the definition . Accessible header node , Tail node , Current node , Chain table size ( It doesn't work )
void read(list* );
void put(list* );
int main(){
list pset;// amount to numset* pset;
pset.head=NULL;
pset.end=NULL;
pset.now=NULL;
pset.size=0;// Initialize linked list . amount to pset=NULL;
read(&pset);
put(&pset);
//free(pset);// When should I use free... I don't understand , Put it here in the back put() In the function free 了
return 0;
}
void read(list* p){
int n;
//numset* position=(numset*)malloc(sizeof(numset));
numset* position=NULL;// Save the current node A1 The location of , When processing to the next node A2, You can assign this position to A2 The forerunner of . Now there are no nodes , The position pointer points to null .
// When do you want to apply for space .. I don't quite understand
while(scanf("%d",&n) && n!=-1){
p->now=(numset*)malloc(sizeof(numset));
p->now->data=n;
p->now->former=position;// Hang the precursor node to the previously saved location
if(!p->head){
p->head=p->now;// If the head node pointer points to null , Then hang the head pointer on the node just entered
}
p->end=p->now;// Hang the tail pointer on the latest input node
p->size++;
// printf("now=%d\n",p->now->data); // Test use . Output the node just read
position=p->now;// Save the current node A1 The location of , When processing to the next node A2, You can assign this position to A2 The forerunner of
p->now=p->now->next;
// printf("former=%d\n",p->now->former); // Test use . Why does it always output the same value here ??? I don't understand
}
}
void put(list* p){
while(p->end){
printf("%d ",p->end->data);
// if(p->end->former) //printf("former=%d\n",p->end->former->data); Test use , The precursor values output here are all right ah ah why ... Crazy ing
free(p->end);
p->end=p->end->former;
}
}
solve ... Why does the output of the precursor node in the two functions get different results ??
边栏推荐
- i++与++i详解
- Don't stick to it for 68 days. Baboons eat bananas
- String, array, generalized table (detailed)
- Two forms of softmax cross entropy + numpy implementation
- 读懂 互联网巨头 【中台之战】 以及 中台 发展思维
- No, just stick to it for 64 days. Find the insertion location
- Introduction and examples of parameters in Jenkins parametric construction
- Machine vision Series 2: vs DLL debugging
- 用 ZEGO Avatar 做一个虚拟人|虚拟主播直播解决方案
- Differences and principles of bio, NiO and AIO
猜你喜欢
Make a virtual human with zego avatar | virtual anchor live broadcast solution
redux快速上手
不会就坚持58天吧 实现前缀树
Basic operation of queue
不会就坚持64天吧 查找插入位置
C语言力扣第61题之旋转链表。双端队列与构造循环链表
6. Pytest generates an allure Report
Record of problems encountered in ROS learning
C language force buckle question 61 of the rotating list. Double ended queue and construction of circular linked list
你真的会写Restful API吗?
随机推荐
Sign the college entrance examination
Mongo shell interactive command window
Oracle update and delete data
leetcode 686.重复叠加字符串 KMP方法(C语言实现)
Leftmost prefix principle of index
Laya中的A星寻路
C语言:联合体知识点总结
pyscript无法引入包
[k210 stepping pit] pytorch model is converted to kmodel and used on dock. (ultimately not achieved)
Post export data, return
WebRTC实现简单音视频通话功能
No, just stick to it for 64 days. Find the insertion location
Record the Niua packaging deployment project
Multi rotor six axis hardware selection
Pytorch fixed random seed & recurrence model
Simply change the picture color
Mongo Shell交互式命令窗口
不会就坚持67天吧 平方根
Exception handling: pyemd or pyemd not found
C language: enumerating knowledge points summary