while(x!=9999){ s = (LNode *)malloc(sizeof(LNode)); s->data = x; r->next = s; r = s; cin>>x; }
时间: 2024-05-19 12:15:58 浏览: 63
This code reads in a series of integers from the user until the value 9999 is entered. For each integer entered, a new node is created with the integer as its data value. The new node is then added to the end of a linked list. The variable "r" keeps track of the last node in the list and is updated each time a new node is added. The input value is stored in the variable "x" and is read in using the "cin" function from the C++ standard library.
相关问题
DLinkList createDLinkList(int n) { DLinkList head = (LNode*)malloc(sizeof(LNode)); head->prev = NULL; head->next = NULL; DLinkList tail = head; for (int i = 0; i < n; i++) { DLinkList p = (LNode*)malloc(sizeof(LNode)); p->data = i + 1; p->prev = tail; p->next = NULL; tail->next = p; tail = p; } return head; }
这段代码是用来创建双向链表的,其中的参数 n 表示需要创建的链表节点个数。它首先创建一个头结点 head,然后将尾节点 tail 初始化为 head。在 for 循环中,它创建一个新的节点 p 并将其插入到尾节点 tail 的后面,然后将 tail 指向 p,以便下一次循环可以将新的节点插入到它后面。最后,它返回头结点 head。这段代码的时间复杂度为 O(n),因为它需要对链表中的每个节点都进行一次插入操作。
Lnode *Creat_Linklist2()//尾插法建立单链表 { Lnode *L=NULL; Lnode *s,*r=L; int x;//设置元素类型为int scanf("%d",&x); while(x!=0)//0结束输入 { s=(Lnode*)malloc(sizeof(Lnode));//申请结点 s->data=x; if(L==NULL)//第一个结点处理 { L=s; } else { r->next=s;//r代表最后一个结点,把s插入该结点之后 } r=s;//让r重新指向最后一个结点 scanf("%d",&x); } if(r!=NULL) { r->next=NULL;//对于非空表,最后结点指针域置空 } printf("成功建立链表!\n"); return L; }什么意思
这段代码是用尾插法创建一个单链表。它首先定义了一个指向链表头节点的指针L,以及两个指向节点的指针s和r。然后它通过循环,输入一些整数,并分配一个新的节点s,将输入的整数存储在s的数据域中。如果这是第一个节点,它将L指向s,否则它将s插入到r之后,然后更新r指针指向s,以便下一次输入。当输入的整数为0时,输入结束。最后,它将最后一个节点的指针域设置为NULL,并返回指向链表头节点的指针L。
阅读全文
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![pdf](https://img-home.csdnimg.cn/images/20241231044930.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20241231045053.png)