Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

#include #include using namespace std; struct ListNode { int info; ListNode* link; }; void insert(ListNode*& head, int x) { ListNode* p = new ListNode{x, NULL};

image text in transcribed

#include #include

using namespace std;

struct ListNode { int info; ListNode* link; };

void insert(ListNode*& head, int x) { ListNode* p = new ListNode{x, NULL};

if (head == NULL || x info) { p->link = head; head = p; } else { ListNode* prev = head; ListNode* cur = head->link;

while (cur != NULL && x > cur->info) { prev = cur; cur = cur->link; }

p->link = prev->link; prev->link = p; } }

void print(ListNode* head) { ListNode* cur = head; while (cur != NULL) { cout info link; } cout

int main() { ListNode* head = NULL; ifstream inFile("testData.txt"); if (!inFile.is_open()) { cout

while (!inFile.eof()) { /ot end of file int i; inFile >> i; //read in an integer

if (!inFile.fail()) insert(head, i); //insert into the linked list else break; }

inFile.close();

cout

cout > x; insert(head, x); //insert into the linked list

cout

Your task: implement insertion sort using linked list. Main.cpp is provided as a function to insert an element into a sorted linked list. Now, extend that to insertion sort. The file data1.txt contains some random integers. Read that file and then conduct insertion sort. Problem formulation/evaluation: Input: data1.txt Output: print out the sorted linked list that is constructed from data1.txt 020125910120

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Database Management An Organizational Perspective

Authors: Richard T. Watson

1st Edition

0471305340, 978-0471305347

More Books

Students also viewed these Databases questions

Question

2. Describe why we form relationships

Answered: 1 week ago

Question

5. Outline the predictable stages of most relationships

Answered: 1 week ago