5. Show the definition necessary to create a node for a linked list of workers (
ID: 638544 • Letter: 5
Question
5. Show the definition necessary to create a node for a linked list of workers (definition in question 4). The data definition for the worker should be in the structure definition already provided and not in the node for the linked list.
6. Write an algorithm that swaps (exchanges) two nodes in a linked list. The nodes are identified by an index which is passed as a parameter along with the name of the array that contains the linked list. For example, if you wanted to swap the two nodes located at index 5 and 8, the call to the function would be
Swap(LinkedList, 5, 8);
Assume a structure definition for the node as:
struct NODE
{
char data[26];
int link;
};
And an array definition of:
struct NODE LinkedList[100];
Explanation / Answer
void Swap(struct NODE* LinkedListHead, int sw1,int sw2)
{
struct NODE* link_bef_sw1,*link_before_sw2;
while(LinkedListHead!=NULL || LinkedListHead->link!=sw1 || LinkedListHead->link!=sw2)
{
LinkedList=LinkedList->link;
}
if(LinkedListHead->link==sw1)
{
link_bef_sw1=LinkedListHead;
}
else
{
link_bef_sw2=LinkedListHead;
}
LinkedListHead=LinkedListHead->link;
while(LinkedListHead!=NULL || LinkedListHead->link!=sw1 || LinkedListHead->link!=sw2)
{
LinkedListHead=LinkedListHead->link;
}
if(LinkedList->link==sw1)
{
link_bef_sw1=LinkedListHead;
}
else
{
link_bef_sw2=LinkedListHead;
}
temp=LinkedList[sw1]->link;
LinkedList[sw1]->link=LinkedList[sw2]->link;
LinkedList[link_bef_sw1]->link=LinkedList[sw2];
LinkedList[link_bef_sw2]->link=LinkedList[sw1];
LinkedList[sw2]->link=temp;
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.