struct node {

int number;

struct node *next;

};

/* add a node to the linked list */

struct node *addnode(int number, struct node *next);

/* preform merge sort on the linked list */

struct node *mergesort(struct node *head);

/* merge the lists.. */

struct node *merge(struct node *head_one, struct node *head_two);

int main(void) {

struct node *head;

struct node *current;

struct node *next;

int test[] = {8, 3, 2, 6, 1, 5, 4, 7, 9, 0};

int i;

head = NULL;

/* insert some numbers into the linked list */

for(i = 0; i < 10; i++)

head = addnode(test[i], head);

/* sort the list */

head = mergesort(head);

/* print the list */

printf(" before after\n"), i = 0;

for(current = head; current != NULL; current = current->next)

printf("%4d\t%4d\n", test[i++], current->number);

/* free the list */

for(current = head; current != NULL; current = next)

next = current->next, free(current);

/* done... */

return 0;

}

/* add a node to the linked list */

struct node *addnode(int number, struct node *next) {

struct node *tnode;

tnode = (struct node*)malloc(sizeof(*tnode));

if(tnode != NULL) {

tnode->number = number;

tnode->next = next;

}

return tnode;

}

/* preform merge sort on the linked list */

struct node *mergesort(struct node *head) {

struct node *head_one;

struct node *head_two;

if((head == NULL) || (head->next == NULL))

return head;

head_one = head;

head_two = head->next;

while((head_two != NULL) && (head_two->next != NULL)) {

head = head->next;

head_two = head->next->next;

}

head_two = head->next;

head->next = NULL;

return merge(mergesort(head_one), mergesort(head_two));

}

/* merge the lists.. */

struct node *merge(struct node *head_one, struct node *head_two) {

struct node *head_three;

if(head_one == NULL)

return head_two;

if(head_two == NULL)

return head_one;

if(head_one->number < head_two->number) {

head_three = head_one;

head_three->next = merge(head_one->next, head_two);

} else {

head_three = head_two;

head_three->next = merge(head_one, head_two->next);

}

return head_three;

}

Showing posts with label

**Merge sort - linked list**. Show all postsShowing posts with label

**Merge sort - linked list**. Show all posts
Subscribe to:
Posts (Atom)

## Labels

- 2d example insertion sort
- A bubblesort routine
- A simple example showing some comparison operators
- Add numbers using command line arguments (CLA)
- Add Pointers
- Add two matrices and store the result
- Add Two numbers without using operator
- Addition of Two Matrices
- Addition of two numbers in C++
- Area and Perimeter of Rectangle
- Area and Perimeter of Square
- Area of Circle
- ARRANGE THE ELEMENTS IN ARRAY IN DESSENDING ORDER
- ATM programing
- Basic example showing constants usage in C
- below and average students
- Binary search
- Bubble sort - linked list
- bubble sort -2
- Bubble sort in string array
- C Program find Positive Negative with Switch Case Without Conditional Operator
- C Program to calcuate interest and total amount at the end of each year
- C Programs
- C++ Programs
- Calculate area of two Triangles
- Calculate Average of Four Numbers in C++
- Calculate bill with discount in C++
- Calculate Electric Energy Bill
- Calculate Electricity Bill with if-else condition
- Calculate publishing conference cost
- calculate the power in watts
- Calculate the Sum
- Check Leap Year in C++
- Check Odd or Even Number in C++
- Complex Number
- Concatenate Two Strings
- Construct Pyramid of Numbers
- Convert Decimal to Hexadecimal Number
- Convert Roman Numeral to Decimal
- Copy One File to Another
- Count no. of students above
- count occurrences of values in an array.
- Count the array elements
- Create Linear Linked List
- Decimal to Binary Conversion
- Deleting Element In An Array
- Determine Palindrome String
- dynamic pointer array
- Example of Stack
- Example of Using Strings in C
- Facebook Page
- Factorial Function In C
- Factorial of Number Using While Loop
- Factorial off a number using "do while" loop
- Factorials with Recursive Non Recursive
- Fibonacci Series using Recursive Function
- Find 2's complement of Binary Number
- find a given number is positive or negative number in C without if statement
- Find address of char
- Find Area of Rectangle in C++
- Find Generic root of number
- Find Greater between two numbers in C++
- Find Inverse of a Given Matrix
- Find Maximum Value in Array
- Find Prime Factor of number
- FIND THE SUM OF DIGIT THREE Numbers
- Generate Pascal's Triangle
- Greatest Common Divisor (GCD) - Recursive Non Recursive
- Greatest of two Numbers - Conditional Operator
- Hanoi Problem Recursive and Non Recursive Functions
- heap sort
- History Of C
- how 2 find a given number is positive or negative number in C without using relational and conditional operator
- Hsort
- Implement Trapezoidal Method
- Income Class Example
- INORDER .
- Inserting Elements In An Array
- insertion sort
- Insertion sort in linked list
- integer
- Integer Operands Result
- Isort
- Largest and smallest number Integers
- Length of string and show it in upper
- Linked List implementation
- lower and reverse order
- Mark list Analysis using Structures
- Matrix Multiplication
- Menu Driven Calculator
- Merge sort - linked list
- Merging The Elements In An Array
- Msort Merge sort
- Multiplication of Two Matrices
- Multiply and Division of two numbers
- Multiply and swap 2 nmbers using bitwise operators
- Ohms law example In C
- Print 1 to 10 Series
- Print a double pyramid
- Print all permutations of a given string
- Print Armstrong No.S Less Than 1000
- Print Second Largest Among Given Three No.S
- Progam that gives length of side of a Triangle
- Program for conversion of Decimal to Roman Number
- Program for demonstration of Tree Operations - INSERTION
- Program for finding the transpose of a martix in sparse form
- Program for rotating circles using maths Function
- PROGRAM TO ARRANGE THE ELEMENTS IN ARRAY IN ASSENDING ORDER
- program to calculate sum all of the elments in an array
- Program to compute difference between two dates
- Program to construct a pyramid of any input numbers
- program to find greatest of 2 numbers without using relational and conditional operators
- Program to find whether a number is odd or even
- Program to Open a file
- Pyramid using nested for loops
- Qcksort
- Qserch
- quick sort
- Quick Sort : array of pointers to structures
- read a file and write to a file
- relational and conditional operator
- Reverse First Letter characters
- Reverse Floyds Triangle
- Reverse words in a String C Program
- reversing a linked list
- Search an array
- Search An Element in Linked List
- SEARCHING OF THE ELEMENTS OF BINARY NUMBER
- Searching the Element in an Array
- selection sort in array
- Selection sort linked list
- shell sort array
- Shsort
- Sorting Strings in Ascending Order
- Square Root of a number by using simple calculations
- Ssort
- Standard Deviation using Function
- string
- String array Qsort
- Sub String Given Main String Position
- Substring Replacement
- Subtract
- Subtraction of Two Matrices
- the Swapping of two Values using Functions
- This program calculates an average of the numbers entered
- To delete n Characters from a given position in a given string
- Total Number of Consonants in a String
- Treesort - string array
- Use of Strlen() Function
- Volume of Box in C++
- WAP to add 1 and subtract 1 from value of a and b (Incremental / Decremental Operators)
- WAP to add entered three digits
- WAP to add two variables
- WAP to concatenate two strings
- WAP to Convert CELCIUS to fahrenheit
- WAP to count number of vowels
- WAP to create double dimension array of 2x3 matrix and display its Elements
- WAP to enter character by using getch ( ) function
- WAP to enter records and also repeat the step if user wants to continue
- WAP to find amount of given quantity of any company with 10% discount using switch case
- WAP to find if number=10 then Good else Bad
- WAP to find out Bigger and Equal number from three numbers (Ternary Operators)
- wap to find out bigger number (if-else)
- WAP to find out Bigger number from two numbers (Ternary Operators)
- WAP to find out Even or Odd number (IF-ELSE)
- WAP to find out Positive or Negative (IF-ELSE)
- WAP to find out power of any number
- WAP to find out Quardratic Equation (d=b2-4ac)
- WAP to find out Square root of any number
- WAP to find out Total Bill with discount according to conditions (Ternary Operators)
- WAP to find out total marks and Percentage of three subjects
- WAP to find out total marks of three subjects
- WAP to find out TOTAL SALARY with (IF-ELSE)
- WAP to find out Year is leap or not (IF-ELSE)
- WAP to find size of any variable
- WAP to find string within a string
- WAP to find that number is palandrom or not (121=121)
- WAP to find that number is prime or not
- WAP to find the factorial of the number (1x2x3x4)
- WAP To find the GCD (greatest common divisor) of two given integers
- WAP to find the length of any string
- WAP TO PRINT ANY MESSAGE ON SCREEN
- WAP to print any name on screen 10 times
- WAP to print ASCII code from 0 to 255
- WAP to print Even and Odd numbers from 20 to 1
- WAP to print fabbonic series from 1 to 55
- WAP to print name and display on screen
- WAP to print numbers from 1-50 which are divided by 7
- WAP to print Odd numbers from 1 to 20
- WAP to print series from 1 to 10 and break on 5
- WAP to print series from 1 to 10 and find its square and cube
- WAP to print series from 1 to 10 and skip 5 and 7
- WAP to print series from 20 to 1
- WAP to print series from start to end using do-while loop
- WAP to print Stars on screen
- WAP to print table of 5
- WAP to print the detail of the programmer
- WAP to print value of multiple data types
- WAP to Reverse of any number using while loop
- Wap to reverse words
- WAP to Sum
- WAP to sum of five elements of an array
- WAP to SWAP the three digit number
- Whether the given no. is armstrong or not
- Whether the given no. Is palindrome or not
- Write a C program to find both the largest and smallest number in a list of integers
- write a c program to find prime numbers
- Write a C program to find the sum of individual digits of a positive integer
- Write a C program to generate all the prime numbers between 1 and n
- Write a C program to print all permutations of a given string
- Write a C program to reverse the words in a sentence in place.
- write a program to find the factorial of a number
- write c program to find the roots of a quadratic equation
- Write C program to implement Simpson method
- Write C Program to Print a Triangle
- XOR list example