The following program is supposed to allocate nodes, put them into a linked list, print them out and then destroy them. Fix the bugs in the program and rewrite the code. Your program should have complete implementation required to run the following function. Take dummy data execute it, take snap shot and paste it in the document. struct linkedList{ int number; linkedList* next; }; void AddNode() { linkedList* head=NULL; linkedList* current; for(int i=0;i<10;i++){ current = new linkedList; current->number = i; current.next = head; head = current; } while(head->next!=NULL){ cout << head->number << endl; current = head->next; delete current; head = current; } return 0; }
#include<iostream>
using namespace std;
struct linkedList{
int number;
linkedList* next;
};
void push(linkedList** head, int data)
{
linkedList* newNode = new linkedList();
newNode->number = data;
newNode->next = (*head);
(*head) = newNode;
}
void printList(linkedList* head){
linkedList * node = head;
while(node != NULL){
cout<<node->number<<" ";
node = node->next;
}
}
void AddNode()
{
linkedList* head=NULL;
for(int i=0;i<10;i++){
push(&head, i);
}
cout<<"The contents of the linked list are: \n";
printList(head);
}
int main(){
AddNode() ;
return 0;
}
Comments
Leave a comment