Question

Exercise 1 In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method...

Exercise 1

In this exercise, you will add a method swapNodes to SinglyLinkedList class. This method should swap two nodes node1 and node2 (and not just their contents) given references only to node1 and node2. The new method should check if node1 and node2 are the same node, etc. Write the main method to test the swapNodes method. Hint: You may need to traverse the list.

Exercise 2

In this exercise, you will use the DoublyLinkedList implementation of the textbook. Write a method for concatenating two doubly linked lists L and M, with header and trailer sentinel nodes, into a single list L′. Write a main method to test the new method. Hint: Connect the end of L into the beginning of M.

Homework Answers

Answer #1

IMPLEMENTATION IN C++

Answer 1:

Source Code:

#include <iostream>
using namespace std;
struct Node {
int data;
struct Node *next;
};
struct Node* head = NULL;
void insert() // This method insert node at the beginning
{
int new_data;
cout<<"\n Enter new Node's Data:";
cin>>new_data;
struct Node* new_node = (struct Node*) malloc(sizeof(struct Node));
new_node->data = new_data;
new_node->next = head;
head = new_node;
}
void display() {
struct Node* ptr;
ptr = head;
while (ptr != NULL) {
cout<< ptr->data <<"-->";
ptr = ptr->next;
}
}

void swapNodes(Node **head_ref)
{
int x,y;
cout<<"Enter the elements you want to swap:";
cin>>x>>y;
// Nothing to do if x and y are same
if (x == y) return;
  
// Search for x (keep track of prevX and CurrX
Node *prevX = NULL, *currX = *head_ref;
while (currX && currX->data != x)
{
prevX = currX;
currX = currX->next;
}
  
// Search for y (keep track of prevY and CurrY
Node *prevY = NULL, *currY = *head_ref;
while (currY && currY->data != y)
{
prevY = currY;
currY = currY->next;
}
  
// If either x or y is not present, nothing to do
if (currX == NULL || currY == NULL)
return;
  
// If x is not head of linked list
if (prevX != NULL)
prevX->next = currY;
else // Else make y as new head
*head_ref = currY;
  
// If y is not head of linked list
if (prevY != NULL)
prevY->next = currX;
else // Else make x as new head
*head_ref = currX;
  
// Swap next pointers
Node *temp = currY->next;
currY->next = currX->next;
currX->next = temp;
cout<<"\nSwapping Done";
}
int main() {
while(1)
{
cout<<"\n1.Insert\n2.Display\n3.Swap\n4.Exit\nEnter your choice:";
int ch;
cin>>ch;
switch(ch)
{
case 1:insert();
break;
case 2:display();
break;
case 3:swapNodes(&head);
break;
case 4: exit(0);
default:cout<<"Invalid choice";
break;
}

}
}

Output:

1.Create and display linked list

2. Swap the nodes

Answer 2:

Source Code:

#include <iostream>
using namespace std;
struct node {
int data;
struct node *prev;
struct node *next;
};

struct node *list = NULL;
struct node *list_last = NULL;

struct node *even = NULL;
struct node *even_last = NULL;

struct node *odd = NULL;
struct node *odd_last = NULL;

struct node *current = NULL;

//Create Linked List
void insert(int data) {
// Allocate memory for new node;
struct node *link = (struct node*) malloc(sizeof(struct node));

link->data = data;
link->prev = NULL;
link->next = NULL;

// If head is empty, create new list
if(data%2 == 0) {
if(even == NULL) {
even = link;
return;
} else {
current = even;

while(current->next != NULL)
current = current->next;

// Insert link at the end of the list
current->next = link;
even_last = link;
link->prev = current;
}
} else {
if(odd == NULL) {
odd = link;
return;
} else {
current = odd;

while(current->next!=NULL)
current = current->next;

// Insert link at the end of the list
current->next = link;
odd_last = link;
link->prev = current;
}
}
}

//display the list
void print_backward(struct node *head) {
struct node *ptr = head;

cout<<"\n[last] <--->";
//start from the beginning
while(ptr != NULL) {
cout<<" <--->"<<ptr->data;
ptr = ptr->prev;
}

cout<<" [head]\n";
}

//display the list
void printList(struct node *head) {
struct node *ptr = head;

cout<<"\n[head] <--->";
//start from the beginning
while(ptr != NULL) {
cout<<" <--->"<<ptr->data;
ptr = ptr->next;
}

cout<<" [last]\n";
}

void combine() {
struct node *link;

list = even;
link = list;

while(link->next!= NULL) {
link = link->next;
}

link->next = odd;
odd->prev = link;

// assign link_last to last node of new list

while(link->next!= NULL) {
link = link->next;
}

list_last = link;
}

int main() {
int i;

for(i = 1; i <= 10; i++)
insert(i);

cout<<"Even : ";
printList(even);

cout<<"Even (R): ";
print_backward(even_last);

cout<<"Odd : ";
printList(odd);

cout<<"Odd (R) : ";
print_backward(odd_last);

combine();

cout<<"Combined List :\n";
printList(list);

cout<<"Combined List (R):\n";
print_backward(list_last);

return 0;
}

OUTPUT:

Even means list contains only even nodes

Odd means list contains only odd nodes.

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an...
urgent !! ASAP PLEASE DATA STRUCTURES To class SinglyLinkedList, add method afterThird(E e) which receives an element e and insert that in a new node after the third node in the list (assume the first node in the list is number 1). You should ensure that the linked list has at least three nodes, otherwise throw an exception. You should consider all possible cases.
Don't forget to have the main method A. Write a class that maintains the top ten...
Don't forget to have the main method A. Write a class that maintains the top ten scores for a game application, implementing the add and remove methods but using a singly linked list instead of an array. B. Perform the previous project, but use a doubly linked list. Moreover, your implementation of remove(i) should make the fewest number of pointer hops to get to the game entry at index i.
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings....
C PROGRAMMING Doubly Linked List For this program you’ll implement a doubly linked list of strings. You must base your code on the doubly linked list implementation given in my Week 8 slides. Change the code so that instead of an ‘int’ each node stores a string (choose a suitable size). Each node should also have a next node pointer, and previous node pointer. Then write functions to implement the following linked list operations: • A printList function that prints...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists...
C++ Goals  Build single linked lists using pointers  Learn how to manipulate linked lists In this lab, you will create simple single linked structures consisting of Node objects. Each node will have a pointer to the next node. You will use a head pointer to keep track of the first node in the linked list, and a tail pointer to keep track of the last node in the linked list. Set both head and tail to NULL when...
In this code, I build a single-linked list using a node class that has been created....
In this code, I build a single-linked list using a node class that has been created. How could I change this code to take data of type T, rather than int. (PS: ignore the fact that IOHelper.getInt won't work for the type T... ie second half of main). Here's my code right now: public class SLList { public SLNode head = null; public SLNode tail = null; public void add(int a) {// add() method present for testing purposes SLNode newNode...
"C language" Take this code and make the minor modification necessary to create a circular linked...
"C language" Take this code and make the minor modification necessary to create a circular linked list (Hint: Store a pointer to the first node in the next pointer of the last node.) Demonstrate that this is working by traversing the list until the first pointer is encountered 3 times. Next redefine the node structure to include a back pointer. This will enable your program to move from front to back and then from back to front. It is not...
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the...
LANGUAGE IS C++ 1- A link based getEntry method requires how many steps to access the nth item in the list? a)n b)n+1 c)n^2 d)n-1 2- When calling the insert or remove methods, what is an disadvantage for the link-based implementation of the ADT List? a)harder to understand b)must shift data c)searching for that position is slower d)takes more memory 3-What is the last step in the insertion process for a linked implementation of the ADT List? a)Connect the new...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class...
ALL CODE MUST BE IN C++ Before you begin, you must write yourself a LinkedList class and a Node class (please name your classes exactly ​as I did here). Please follow the below specifications for the two classes. Node.cpp ● This must be a generic class. ● Should contain a generic member variable that holds the nodes value. ● Should contain a next and prev Node* as denoted here. ● All member variables should be private. ● Use public and...
The main goal is to implement two recursive methods, each is built to manipulate linked data...
The main goal is to implement two recursive methods, each is built to manipulate linked data structures. To host these methods you also have to define two utterly simplified node classes. 1.) Add a class named BinaryNode to the project. This class supports the linked representation of binary trees. However, for the BinaryNode class Generic implementation not needed, the nodes will store integer values The standard methods will not be needed in this exercise except the constructor 2.) Add a...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists...
Queues, Lists, and Stacks Assignment: Write client methods to do the following Assume that there exists in main a list   declared as : List<Integer> aList   =   new   List<Integer>(50); Write a client method called bubbleSort which given a list as a parameter uses the bubble sort algorithm to sort the contents of the list. YOU MAY ASSUME that there exists a method called swap (with the following signature) which swaps the contents of two elements: // swap public static void swap(List<Integer>...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT