Answer to Question #236649 in C++ for zain

Question #236649

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; }


1
Expert's answer
2021-09-13T12:49:28-0400
#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;
	}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
New on Blog
APPROVED BY CLIENTS