i need help finishing the program, also i need help checking what i have so far
ID: 3573941 • Letter: I
Question
i need help finishing the program, also i need help checking what i have so far is wrong or right
void findmaxmin(int & max, int & min)
{
node * q;
q = list;
while(q != null)
{
max = min = q -> info;
if (q -> info > max)
max = q -> info;
if (q -> info < min)
min = q -> info;
q = q -> next;
}
}
void llist :: iment_last()
{
int value;
cout << "Enter the value to be imeted:";
cin>> value;
struct node *temp, *s;
temp = create_node(value);
s = start;
while (s-> next != NULL)
{
s = s -> next;
}
temp -> next = NULL;
s-> next = temp;
}
void llist :: iment_begin ()
{
int value;
cout << "Enter the value to be imeted:";
cin>> value;
struct node *temp, *p;
temp = create_node(value);
if (start == null)
{
start = temp;
start -> next = NULL;
}
else
{
p = start;
start = temp;
start -> next =p;
}
}
node *iment (node *head, int value)
{
if (head == NULL)
{
head = malloc (sizeof(node));
if ( head == NULL)
{
cout << "failed to create head node";
return head;
}
head -> value = value;
head -> next = NULL;
return head;
}
node *newNode;
newNode= malloc (sizeof(node));
if (newNode == NULL)
{
cout << "failed to create node";
return newNode;
}
newNode -> value = value;
newNode -> next = NULL;
if (value < head -> value)
{
newNode -> next = head;
return newNode;
}
node *temp = NULL;
temp = head;
while (temp -> next != NULL && temp -> value < value)
{
temp = temp -> next;
}
newNode -> next = temp -> next;
temp -> next = newNode;
return head;
}
Explanation / Answer
Tested on Linux,Ubuntu
/*********************LinkedListOperation.cpp**********************/
#include <iostream>
#include <fstream>
#include <malloc.h>
using namespace std;
//structure declaration
struct Node
{
int number;
struct Node* next;
};
struct Node *head=NULL;
//This function is used to get Node which has maximum value
Node* findMax(struct Node *firstNode) {
//variable declarations
struct Node *temp=firstNode,*maxNode;
int max=0;
//run the loop until temp become NULL
while(temp!=NULL) {
if(temp->number>max) {
max=temp->number;
maxNode=temp;
}
temp=temp->next;
}
//returning max Node
return maxNode;
}
//This function is used to delete a node which has minimum value in list
void deleteMin(struct Node **firstNode) {
//variable declaration and initialization
struct Node *temp=*firstNode,*smallest=*firstNode,*prev;
while(temp!=NULL){
//checking if temp has next node and next node value less then smallest node value
if(temp->next!=NULL&&temp->next->number<smallest->number) {
smallest=temp->next;
prev=temp;
}
temp=temp->next;
}
//if it is not head Node
if(smallest!=*firstNode) {
prev->next=smallest->next;
} else{
(*firstNode)=(*firstNode)->next;
}
}
//This function is used to sort linked list
void sortListAscending(struct Node **firstNode) {
//variable declaration
struct Node *q,*p;
int data;
q=*firstNode;
//sorting logic
while(q!=NULL)
{
p=q->next;
while(p!=NULL)
{
if(q->number>p->number)
{
data=q->number;
q->number=p->number;
p->number=data;
}
p=p->next;
}
q=q->next;
}
}
/*Adding Node at front*/
void addFirst(struct Node** head, int num)
{
if ((*head) == NULL) { // checking if first node is NULL or not
(*head) = (struct Node*)malloc(sizeof(struct Node));
(*head)->number = num;
(*head)->next = NULL;
}
else {
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node)); //dynamically allocation of memory
newNode->number = num;
newNode->next = *head;
*head = newNode;
}
}
//AddNodetoEnd Method Implementation
void addLast(struct Node **lastNode, int num){
if((*lastNode)==NULL){// checking if first node is NULL or not
(*lastNode)=(struct Node*)malloc(sizeof(struct Node));
(*lastNode)->number=num;
(*lastNode)->next=NULL;
}else{
struct Node *temp=*lastNode;
while(temp->next!=NULL){
temp=temp->next;
}
struct Node *newNode=(struct Node*)malloc(sizeof(struct Node));//dynamically allocation of memory
newNode->number=num;
newNode->next=NULL;
temp->next=newNode;
}
}
/*addInOrder method implementation */
Node * addInOrder(Node *currNode, int num){
if(currNode==NULL){// checking if first node is NULL or not
currNode=(struct Node*)malloc(sizeof(struct Node));//dynamically allocation of memory
currNode->number=num;
currNode->next=NULL;
}
else{
struct Node *prev=currNode,*temp=currNode;
while(temp!=NULL){
if(temp->number>num)
break;
else{
prev=temp;
temp=temp->next;
}
}
struct Node* newNode=(struct Node*)malloc(sizeof(struct Node));//dynamically allocation of memory
newNode->number=num;
newNode->next=temp;
prev->next=newNode;
}
}
/*Display List Method Implementation*/
void printList (struct Node* firstNode){
while(firstNode!=NULL){
cout<<firstNode->number<<" ";
firstNode=firstNode->next;
}
cout<<endl;
}
int main() {
//variable declaration
std::string filename;
int data;
cout<<"Please Enter file Name: ";
cin>>filename;
std::ifstream file(filename.c_str());
// Make sure the file stream is good
if(!file) {
cout << endl << "Failed to open file " << filename;
return 1;
}
int i;
while (file >> i) {
//initially adding node at front side
addFirst(&head,i);
}
//printing list
printList(head);
//adding number at END of List
addLast(&head,10);
addLast(&head,11);
addLast(&head,12);
//printing list
cout<<"Printing List After calling AddLast function"<<endl;
printList(head);
cout<<"Calling finMax Function"<<endl;
struct Node *temp=findMax(head);
cout<<"Max Number is: "<<temp->number<<endl;
cout<<"Deleting minimum value from Node"<<endl;
deleteMin(&head);
printList(head);
cout<<"Sorting Linked List by sortListAscending function"<<endl;
sortListAscending(&head);
printList(head);
cout<<"Calling AddInOrder function"<<endl;
cout<<"Please Enter number which you wana insert"<<endl;
cin>>data;
addInOrder(head,data);
printList(head);
return 0;
}
/*********************output*******************/
lalchand@lalchand:~/Desktop/chegg$ g++ LinkedListOperation.cpp
lalchand@lalchand:~/Desktop/chegg$ ./a.out
Please Enter file Name: test.txt
14 8 7 6 5 4 3 2 1
Printing List After calling AddLast function
14 8 7 6 5 4 3 2 1 10 11 12
Calling finMax Function
Max Number is: 14
Deleting minimum value from Node
14 8 7 6 5 4 3 2 10 11 12
Sorting Linked List by sortListAscending function
2 3 4 5 6 7 8 10 11 12 14
Calling AddInOrder function
Please Enter number which you wana insert
9
2 3 4 5 6 7 8 9 10 11 12 14
/*******************test.txt**************/
1
2
3
4
5
6
7
8
14
Thanks a lot
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.