当前位置:网站首页>[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 ??
边栏推荐
- Pyqt5 learning pit encounter and pit drainage (3) background picture coverage button style and check button status
- Oracle update and delete data
- Won't you just stick to 62 days? Sum of words
- 不会就坚持64天吧 查找插入位置
- C语言:结构体简单语法总结
- Dabao and Erbao
- Use of torch.optim optimizer in pytorch
- Pyscript cannot import package
- TypeError: Cannot read properties of undefined (reading ‘then‘)
- MySQL - 深入解析MySQL索引数据结构
猜你喜欢
随机推荐
MySQL - 聚簇索引和辅助索引
Basic operation of queue
C language: enumerating knowledge points summary
Redux quick start
Christmas tree web page and Christmas tree application
异常解决:cococaption包出现找不到edu.stanford.nlp.semgraph.semgrex.SemgrexPattern错误
Use of construction methods
C language: typedef knowledge points summary
C语言:浅谈各种复杂的声明
15.federation
Introduction and examples of parameters in Jenkins parametric construction
The daily life of programmers
Idea small settings
你真的会写Restful API吗?
JVM (heap and stack) memory allocation
15.federation
Jenkins 参数化构建中 各参数介绍与示例
Laya中的A星寻路
No, just stick to it for 64 days. Find the insertion location
Why is it necessary to scale the attention before softmax (why divide by the square root of d_k)









