Question
IN C LANGUEGA(PROGRAMİN C)
a) [90 marks] Write a program that .Splits a list into two halves. The function split must modify the original list and retur
0 0
Add a comment Improve this question Transcribed image text
Answer #1

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

struct Node{
int data;
struct Node*next;
};


void insert_node(struct Node**,int);
void show_data(struct Node*);
struct Node* split(struct Node**);
struct Node* merge(struct Node**,struct Node**);

void insert_node(struct Node**root,int value){

struct Node*new_node=(struct Node*)malloc(sizeof(struct Node));

if(new_node)
{   

new_node->data=value;
new_node->next=NULL;

if(*root==NULL){
*root=new_node;
}
else{
struct Node*temp=NULL;
temp=*root;
while(temp->next){
temp=temp->next;
}
temp->next=new_node;
  
}
}
else
{
printf("\n Memory overflow please try again..!");
}
}


void print_list_data(struct Node*temp){

if(temp==NULL){
}
else{   
while(temp){
printf("%d ",temp->data);
temp=temp->next;
}
}
}

struct Node* split(struct Node**root){
   if(*root==NULL){
printf("\n No element in this linked list ");
return NULL;

}
int size = 1;
struct Node*temp=*root;

while(temp->next!=NULL){
temp=temp->next;
++size;
}
if(size%2!=0){
insert_node(root,99);
++size;
}
temp=*root;
for (int i =0; i<size/2; i++){
temp=temp->next;
}
return temp;
}

struct Node* merge(struct Node**list1, struct Node**list2){
   if(*list1==NULL){
/* No element in first link list */
return *list2;
}
struct Node*temp=*list1;

while(temp->next!=NULL){
temp=temp->next;
}
temp->next = *list2;
return *list1;
}


int main(){

/* start program execution are here */
struct Node*root=NULL;
  
  
/* 34 ->234 ->334 ->NULL*/
  
insert_node(&root,34);
insert_node(&root,234);
insert_node(&root,334);
  
printf("\n\n Original Linked List:");
print_list_data(root);

struct Node*back = split(&root);
printf("\n\n Second Half Of Linked List: ");
/* 34 ->234 ->334 ->99 ->NULL*/
print_list_data(back);

struct Node*list1=NULL;
  
/* 1 ->2 ->3 ->NULL*/
  
insert_node(&list1,1);
insert_node(&list1,2);
insert_node(&list1,3);
  
struct Node*list2=NULL;
  
/* 4 ->5 ->5 ->NULL*/
  
insert_node(&list2,4);
insert_node(&list2,5);
insert_node(&list2,6);
  
struct Node*mergedlist = merge(&list1,&list2);
printf("\n\n Merged Linked List: ");
/* 34 ->234 ->334 ->99 ->NULL*/
print_list_data(mergedlist);
/*end execution*/
return 0;
}

too linked. list :- access us ssible , ten aold, ess mode a psov.ded Qw M twoinkad List, we are not Tust we are So, ve have t

Add a comment
Know the answer?
Add Answer to:
A) [90 marks] Write a program that .Splits a list into two halves. The function split must modify...
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. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an...

    Must be C++ 11 Write a function named insertinArray that takes the following parameters: list: an integer array index: index at which to insert the new item numitems: number of items currently in the array arrayCapacity: capacity of the array newVal: value to insert into the array If the array is at capacity then the function should return-1. Otherwise, insert newVal at index and return O for success int insertInArray(int listl 1, int index, int numItems, int arrayCapacity, int newVal)...

  • I need this in C++. This is all one question Program 2: Linked List Class For...

    I need this in C++. This is all one question Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we'll expand its functionality and make it a doubly linked list with the ability to traverse in both directions. Since the list is doubly linked, each node will have the following structure: struct...

  • c++ only Program 2: Linked List Class For this problem, let us take the linked list...

    c++ only Program 2: Linked List Class For this problem, let us take the linked list we wrote in a functional manner in a previous assignment and convert it into a Linked List class. For extra practice with pointers we’ll expand its functionality and make it a doubly linked list with the ability to traverse in both directions.    Since the list is doubly linked, each node will have the following structure: struct Node { int number; Node * nextNode;...

  • Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that...

    Using Racket Recursion, tail-recursion, high-order functions and functional programming. 1. Modify our filter function so that it is tail-recursive. You may use the letrec form but do not use any additional forms and functions besides those we've talked about in class. (define filter (lambda (input-list func)     (cond ((null? input-list) '())           ((func (car input-list))            (cons (car input-list) (filter (cdr input-list) func)))           (else            (filter (cdr input-list) func))))) 2. Test your filter function on '(25 -22 44 56...

  • starter code To write a program using the starter code which is TestLinkedList to see if...

    starter code To write a program using the starter code which is TestLinkedList to see if the LinkedList program has bugs. It will produce ether a pass or fail.More information is in the first two pictures. LinkedList.java /** * @author someone * * Implements a double-linked list with four errors */ public class LinkedList<E> { // The first and last nodes in the list private Node<E> head, tail; // Number of items stored in the list private int size; //...

  • Requirements Print a range Write a bag member function with two parameters. The two parameters are...

    Requirements Print a range Write a bag member function with two parameters. The two parameters are Items x and y. The function should write to the console all Items in the bag that are between the first occurrence of x and the first occurrence of y. You may assume that items can be compared for equality using ==. Use the following header for the function: void print_value_range(const Item& x, const Item& y); print_value_range can be interpreted in a number of...

  • Hi I need a fix in my program. The program needs to finish after serving the...

    Hi I need a fix in my program. The program needs to finish after serving the customers from the queue list. Requeriments: Headers: DynamicArray.h #ifndef DynamicArray_h #define DynamicArray_h #include using namespace std; template class DynamicArray { V* values; int cap; V dummy; public: DynamicArray(int = 2); DynamicArray(const DynamicArray&); ~DynamicArray() { delete[] values; } int capacity() const { return cap; } void capacity(int); V operator[](int) const; V& operator[](int); DynamicArray& operator=(const DynamicArray&); }; template DynamicArray::DynamicArray(int cap) { this->cap = cap; values =...

  • How to write the insert, search, and remove functions for this hash table program? I'm stuck......

    How to write the insert, search, and remove functions for this hash table program? I'm stuck... This program is written in C++ Hash Tables Hash Table Header File Copy and paste the following code into a header file named HashTable.h Please do not alter this file in any way or you may not receive credit for this lab For this lab, you will implement each of the hash table functions whose prototypes are in HashTable.h. Write these functions in a...

  • Creating linked list data structure Overview The purpose of this assignment is for you to write...

    Creating linked list data structure Overview The purpose of this assignment is for you to write a data structure called a Linked List, which utilizes templates (similar to Java’s generics), in order to store any type of data. In addition, the nature of a Linked List will give you some experience dealing with non-contiguous memory organization. This will also give you more experience using pointers and memory management. Pointers, memory allocation, and understand how data is stored in memory will...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT