在C ++中将单链表转换为循环链表
在本教程中,我们将讨论将单链接列表转换为循环链接列表的程序。
为此,我们将提供一个单链表。我们的任务是获取该列表的元素,并将其转换为循环链接列表。
示例
#include <bits/stdc++.h>
//node structure of linked list
struct Node {
int data;
struct Node* next;
};
//converting singly linked list
//to circular linked list
struct Node* circular(struct Node* head){
struct Node* start = head;
while (head->next != NULL)
head = head->next;
//assigning start to the head->next node
//if head->next points to NULL
head->next = start;
return start;
}
void push(struct Node** head, int data){
//creation of new node
struct Node* newNode = (struct Node*)malloc
(sizeof(struct Node));
//putting data in new node
newNode->data = data;
newNode->next = (*head);
(*head) = newNode;
}
//displaying the elements of circular linked list
void print_list(struct Node* node){
struct Node* start = node;
while (node->next != start) {
printf("%d ", node->data);
node = node->next;
}
printf("%d ", node->data);
}
int main(){
struct Node* head = NULL;
push(&head, 15);
push(&head, 14);
push(&head, 13);
push(&head, 22);
push(&head, 17);
circular(head);
printf("Display list: \n");
print_list(head);
return 0;
}输出结果
Display list: 17 22 13 14 15
热门推荐
10 八一幼儿祝福语大全简短
11 公司乔迁食堂祝福语简短
12 婚礼结束聚餐祝福语简短
13 儿媳买车妈妈祝福语简短
14 毕业送礼老师祝福语简短
15 同事辞职正常祝福语简短
16 恭贺新婚文案祝福语简短
17 金店立秋祝福语简短英文
18 婆婆高寿祝福语大全简短