chmod: WARNING: can\'t access prog.cpp chmod: WARNING: can\'t access ans.cpp //-
ID: 3733425 • Letter: C
Question
chmod: WARNING: can't access prog.cpp
chmod: WARNING: can't access ans.cpp
//------------------------------------------------------------------------
// Directions: COMPLETE the code to implement bubble sort on a linked list:
// (1) Complete the SwapValue function
// (2) Complete the for loop in function LL_BubbleSort
// (3) Complete in inner while loop in function LL_BubbleSort.
// (4) Call LL_BubbleSort in main.
//------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;
struct NODE{int val; NODE * next; NODE(int v, NODE * n){val=v; next=n;}};
void Show(NODE * H)
{
NODE * p = H;
while (p != NULL) {cout << ' ' << p->val; p = p->next;}
}
void SwapValue (NODE * x, NODE * y)
{
//-| (1)
}
void LL_BubbleSort (NODE * H)
{
int pass=1, listsize=0;;
NODE * p, * q = NULL, * prev = NULL;
//-| Determine SIZE of list.
p = H;
while (p != NULL) { listsize++; p = p-> next;}
if (listsize <= 1) return;
//-| (2)
for (pass=1; ; )
{
prev = H;
q = H->next;
while ( q != NULL)
{
//-| (3)
prev = q;
q = q->next;
}//q-loop
}//p-loop
}//LL_Bubblesort
int main()
{
int n, A[]={1,2,6,8,5,7,9,5,9,4,15};
NODE * LIST = NULL;
cin >> n;
for (int k=1; k<=n; k++)
LIST = new NODE(A[k-1], LIST);
cout << endl << "BEFORE: "; Show(LIST); // DO NOT BOTHER!!
//-| (4)
cout << endl << "AFTER: "; Show(LIST); // DO NOT BOTHER!!
cout << endl; return 0; // DO NOT BOTHER!!
}
==============
==> (1) File 'prog.cpp' has been placed in the testing directory.
==> (2) Work in a different window, in the testing directory
==> (3) Edit, compile and test the code in file prog.cpp.
==============
Explanation / Answer
Modified the code and commented for explaination where the code is filled in
//------------------------------------------------------------------------
// Directions: COMPLETE the code to implement bubble sort on a linked list:
// (1) Complete the SwapValue function
// (2) Complete the for loop in function LL_BubbleSort
// (3) Complete in inner while loop in function LL_BubbleSort.
// (4) Call LL_BubbleSort in main.
//------------------------------------------------------------------------
#include <iostream>
#include <cstdlib>
using namespace std;
struct NODE{int val; NODE * next; NODE(int v, NODE * n){val=v; next=n;}};
void Show(NODE * H)
{
NODE * p = H;
while (p != NULL) {cout << ' ' << p->val; p = p->next;}
}
void SwapValue (NODE * x, NODE * y)
{
//Swapping the value in the node and keeping the links intact.
//-| (1)
int temp = x->val;
x->val = y->val;
y->val = temp;
}
void LL_BubbleSort (NODE * H)
{
int pass=1, listsize=0;
NODE * p, * q = NULL, * prev = NULL;
//-| Determine SIZE of list.
p = H;
while (p != NULL) { listsize++; p = p-> next;}
if (listsize <= 1) return;
//-| (2)
// we will iterate listsize-1 number of times as the number of comparisons
// required are listsize-1.
for (pass=1; pass < listsize ; pass++ )
{
prev = H;
q = H->next;
while ( q != NULL)
{
//-| (3)
//Here q is next of prev, that is comes after prev. If value in q is lesser than prev
// then the list is unsorted. Thus we will swap.
if(q->val < prev->val) {
SwapValue (q,prev);
}
prev = q;
q = q->next;
}//q-loop
}//p-loop
}//LL_Bubblesort
int main()
{
int n, A[]={1,2,6,8,5,7,9,5,9,4,15};
NODE * LIST = NULL;
cin >> n;
for (int k=1; k<=n; k++)
LIST = new NODE(A[k-1], LIST);
cout << endl << "BEFORE: "; Show(LIST); // DO NOT BOTHER!!
//-| (4)
// LIST object is already created we just give the call to function
// by passing the LIST as argument.
LL_BubbleSort(LIST);
cout << endl << "AFTER: "; Show(LIST); // DO NOT BOTHER!!
cout << endl; return 0; // DO NOT BOTHER!!
}
Sample output1:
$ ./a.out
5
BEFORE: 5 8 6 2 1
AFTER: 1 2 5 6 8
Sample output 2:
$ ./a.out
9
BEFORE: 9 5 9 7 5 8 6 2 1
AFTER: 1 2 5 5 6 7 8 9 9
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.