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.
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...
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...
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...
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?
extra.cpp. It defines the function extra which will be used in the main program. extra.h. This...
extra.cpp. It defines the function extra which will be used in the main program. extra.h. This is the header file. Notice that is only really provides the declaration of the function. o In the declaration, the names of the parameters are not required, only their types. o Note that the function declaration ends in a ; (semicolon). Don't forget!!! o There are some weird # statements, we'll get to that. • main.cpp. This is the main program. Notice that it...
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).
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT