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.
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?
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum...
Double Linked Lists: Implement the PutItem and DeleteItem function // ItemType.h #ifndef ITEMTYPE_H #define ITEMTYPE_H enum RelationType { LESS, GREATER, EQUAL}; class ItemType { public:     ItemType();     void Initialize(int number);     int getValue() const;     RelationType ComparedTo(ItemType); private:     int value; }; #endif // ITEMTYPE_H // ItemType.cpp #include "ItemType.h" ItemType::ItemType() {     value = 0; } void ItemType::Initialize(int number) {     value = number; } RelationType ItemType::ComparedTo(ItemType otherItem) {         if(value < otherItem.value)             return LESS;         else if...
Discuss a contemporary challenge in healthcare and strategies you would implement to address the issue as...
Discuss a contemporary challenge in healthcare and strategies you would implement to address the issue as a healthcare administrator?
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...
Please implement the java method addInOrder() that allows you to create and maintain the lists in...
Please implement the java method addInOrder() that allows you to create and maintain the lists in the order required. The addInOrder method must work with different external Comparator objects. (Hint: Consider creating and using a private compare method and a private Comparator reference as members of your SLL class. If your SLL is constructed without any parameter, then you should initialize the internal Comparator object reference to null. Otherwise, you should initialize it to the external Comparator object passed as...
Consider a system that could function as a Cloud solution. In order for that system to...
Consider a system that could function as a Cloud solution. In order for that system to work as needed, several functional and nonfunctional requirements will be required. Write a 2-3 page paper detailing at least 3 functional requirements and three nonfunctional requirements required for the Cloud solution. Describe what they will do and why are they essential to the system? Make sure those requirements take into consideration resource requirements, network requirements, and security requirements (attacks, mitigations, vulnerabilities).
You are given 2 sorted sequences of log(n) and n-1 keys. We would like to merge...
You are given 2 sorted sequences of log(n) and n-1 keys. We would like to merge those 2 sorted sequences by performing o(n) comparisons.[Note that we are interested in the comparisons and not the running time.] Show how this can be done or argue how this cannot be done. In class we show that ordinary merging would require no more than lg(n)+n-1+1 = n+lg(n) comparisons.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT