Question

In: Computer Science

EXTRA CHALLENGE: Could you implement a merge lists function which merges n lists in order? This...

EXTRA CHALLENGE: Could you implement a merge lists function which merges n lists in order? This function would accept a pointer to an array of n pointers, where each pointer refers to a list.

Solutions

Expert Solution

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <stdbool.h>

struct node {
int data;
int key;
struct node *next;
};

struct node *head = NULL;
struct node *current = NULL;

//display the list
void printList() {
struct node *ptr = head;
printf("\n[ ");
  
//start from the first
while(ptr != NULL) {
printf("(%d,%d) ",ptr->key,ptr->data);
ptr = ptr->next;
}
  
printf(" ]");
}

//insert link at the first location
void insertFirst(int key, int data) {
//create a link
struct node *link = (struct node*) malloc(sizeof(struct node));
  
link->key = key;
link->data = data;
  
//point it to old first node
link->next = head;
  
//point first to new first node
head = link;
}


//checking whether list is empty
bool isEmpty() {
return head == NULL;
}

int length() {
int length = 0;
struct node *current;
  
for(current = head; current != NULL; current = current->next) {
length++;
}
  
return length;
}


void sort() {

int i, j, k, tempKey, tempData;
struct node *current;
struct node *next;
  
int size = length();
k = size ;
  
for ( i = 0 ; i < size - 1 ; i++, k-- ) {
current = head;
next = head->next;
      
for ( j = 1 ; j < k ; j++ ) {   

if ( current->data > next->data ) {
tempData = current->data;
current->data = next->data;
next->data = tempData;

tempKey = current->key;
current->key = next->key;
next->key = tempKey;
}
          
current = current->next;
next = next->next;
}
}   
}

void main() {
int a[5]={8,6,7,2,9};
int length=sizeof(a)/sizeof(int);
for(int i=0;i<length;i++)
insertFirst(i,a[i]);

printf("Original List: ");
printf("Length",%d);
  
//print the list
printList();


printf("\n");
sort();
  
printf("Sorted List : ");
printList();
  

}


Related Solutions

In order to practice on Linked Lists, you will implement one from scratch which will allow...
In order to practice on Linked Lists, you will implement one from scratch which will allow you to examine how they work and explore their capabilities and limitations. You will build a doubly-circular linked list. In doubly linked lists, each node in the list has a pointer to the next node and the previous node. In circular linked lists, the tail node’s next pointer refers to the head and the head node’s previous pointer refers to the tail rather than...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop...
Loop invariants: Consider the following Python function that merges two sorted lists. Here is a loop invariant for loop 1: “the contents ofnewlistare in sorted order,newlistcontainsaelements oflistAandbelements oflistB” def mergeSortedLists(listA, listB):  newlist = [ ]  a = 0  b = 0  # loop 1  while a < len(listA) and b < len(listB):   if listA[a] < listB[b]:    newlist.append(listA[a])    a +=1   else:    newlist.append(listB[b])    b +=1  if a < len(listA):   newlist.extend(listA[a:])  if b < len(listB):   newlist.extend(listB[b:])  return newlist (a) Write down (in regular...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted...
Recall that in MergeSort algorithm, we used a linear-time subroutine called Merge which merges two sorted lists into a single sorted list. Now suppose there are k sorted lists and there are n elements in total in these k lists. Design an efficient algorithm that merges these k sorted lists into one sorted list. The running time of your algorithm should be a function of both n and k.
Write a recursive function to implement the Factorial of n (n!). In the main, let the...
Write a recursive function to implement the Factorial of n (n!). In the main, let the user input a positive integer and call your function to return the result.
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM] =...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them...
Given two integer arrays sorted in the ascending order, code the function SortArrays to merge them into one array in the descending order. You need to make sure if the values in the arrays are changed, it will still work. (25 points) #include <iostream> using namespace std; void SortArrays (int a[], int b[], int c[], int size); void printArray(int m[], int length); const int NUM = 5; int main() { int arrA[NUM] = {-2, 31, 43, 55, 67}; int arrB[NUM]...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me...
Implement function matmul() that embodies multiplication of n*n matrix in c language?(code) Can you let me know?
In this Java program you will implement your own doubly linked lists. Implement the following operations...
In this Java program you will implement your own doubly linked lists. Implement the following operations that Java7 LinkedLists have. 1. public DList() This creates the empty list 2. public void addFirst(String element) adds the element to the front of the list 3. public void addLast(String element) adds the element to the end of the list 4. public String getFirst() 5. public String getLast() 6. public String removeLast() removes & returns the last element of the list. 7. public String...
A Trigonometric Polynomial of order n is a function of the form: ?(?) = ?0 +...
A Trigonometric Polynomial of order n is a function of the form: ?(?) = ?0 + ?1 cos ? + ?1 sin ? + ?3 cos(2?) + ?2sin(2?) + ⋯ + ?ncos(??) + ?nsin (??) 1) Show that the set {1, cos ? , sin ? , cos(2?) , sin(2?)} is a basis for the vector space ?2 = {?(?) | ?(?)?? ? ????????????? ?????????? ?? ????? ≤ 2} < ?, ? > = ∫ ?(?)?(?)?? defines an inner-product on...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT