在C ++编程中编写函数以删除链接列表
在这里,我们将创建一个函数,该函数将删除链接列表中的所有元素。
在c/c++中,没有执行此任务的特定功能,但是在Java中,完成了自动垃圾回收以简化删除链表的过程。
现在,让我们看看该程序的实现,
示例
#include <iostream>
using namespace std;
class Node{
public:
int data;
Node* next;
};
void deleteLinkedList(Node** head_ref){
Node* current = *head_ref;
Node* next;
while (current != NULL){
cout<<current->data<<"\t";
next = current->next;
free(current);
current = next;
}
*head_ref = NULL;
}
void push(Node** head_ref, int new_data){
Node* new_node = new Node();
new_node->data = new_data;
new_node->next = (*head_ref);
(*head_ref) = new_node;
}
int main(){
Node* head = NULL;
push(&head, 25);
push(&head, 10);
push(&head, 5);
push(&head, 90);
push(&head, 68);
cout<<"Elements of linked list : ";
deleteLinkedList(&head);
cout << "\nLinked list deleted";
}输出结果
Elements of linked list : 68 90 5 10 25 Linked list deleted
热门推荐
10 小红书平安祝福语简短
11 生日祝福语大全女孩简短
12 收生日红包祝福语 简短
13 领证幽默祝福语简短
14 法考面试祝福语简短
15 老哥出门祝福语简短语
16 送灯祝福语简短独特
17 幼儿狗年祝福语大全简短
18 好听的元旦简短祝福语