## Monday, October 14, 2013

### Linked List create, insert, sort, min, max, find and print complete program

I have been asked a few times to provide a complete working example of different operations on Linked List.

This post is an attempt to do just that.

#include <stdio.h>
#include <stdlib.h> // for malloc

// make sure to typedef to give better name
typedef struct ll
{
int data;
struct ll *next;

// remember to declare functions that will be used in main but defined after it

int main(int argc, char* argv[])
{
int max = 0, min = 0;

// de fine array
int input[] = { 43, 22, 11, 4, 22, 56, 47, 21, 78, 99, 97 };

// find the array length in C
int inputSize = sizeof(input) / sizeof(input[0]);

// add elements to the beginning of the list

// Print list

// add elements to the end of the list

// Print list

// insert a single element at beginning
inOrderList = InsertElementAtBeginning(100, inOrderList);

// Print list

// insert a single element at end
inOrderList = InsertElementAtEnd(200, inOrderList);

// Print list

// Search for an element
ele = Find(56, inOrderList);

if (ele != NULL) {
printf("\r\nElement found: %d", ele->data);
}

// Find max and min in the list
MaxMinInList(&max, &min, inOrderList);

printf("\r\nMax: %d, Min: %d", max, min);

// sort the list
Sort(inOrderList);

// Print list

return 0;
}

{
// define loop variable
int i = 0;

for (i = 0; i < inputSize; i++)
{
// create a temp node
temp->data = input[i];

// add to the beginning of the list
// make this new node point to the current beginning of the list
// NOW set the head of the list to this new node
}

}

{
// define loop variable
int i = 0;

// define the end of the list
// here is where new elements will be inserted

for (i = 0; i < inputSize; i++)
{
// create a temp node
temp->data = input[i];
// make sure to null the pointer to the last element
temp->next = NULL;

// if this is the first element, make head point to it
if (i == 0) {
}
else {
// add to the end of the list
end->next = temp;
}

// NOW set the end of the list to this new node
end = temp;
}

}

{
// create a temp node
temp->data = input;

// add to the beginning of the list
// make this new node point to the current beginning of the list
// NOW set the head of the list to this new node

}

{

// create a temp node
temp->data = input;
temp->next = NULL;

while (curr->next != NULL)
curr = curr->next;

// add it as the last node
curr->next = temp;

}

{

// traverse the entire list
for (list = head; list->next != NULL; list = list->next)
{
// compare to the list ahead
for (pass = list->next; pass != NULL; pass = pass->next)
{
// compare and swap
if (list->data > pass->data)
{
// swap
int temp = list->data;
list->data = pass->data;
pass->data = temp;
}
}
}
}

// finds the first node with the specified value
{
// start at the root

// loop through the entire list
while (currentNode != NULL)
{
// if we have a match
if (currentNode->data == value)
return currentNode;
else // move to the next element
currentNode = currentNode->next;
}
}

// finds the maximum and minimum in the list
{
// start at the root

if (curr == NULL)
return 0; // list is empty

// initialize the max and min values to the first node
*max = *min = curr->data;

// loop through the list
for (curr = curr->next; curr != NULL; curr = curr->next)
{
if (curr->data > *max)
*max = curr->data;
else if (curr->data < *min)
*min = curr->data;
}
}

// print the list
{
printf("\r\nName: %s", name);

// loop while we have data
while (node != NULL)
{
printf(" -> %d", node->data);
// move to next node
node = node->next;
}
}

1. Thanks for sharing your info. I really appreciate your efforts and I will be waiting for your further write.
Thanks for sharing !
five night at freddys 2 | five night at freddys 4 |
2048 game online| fireboy and watergirl | tanki online 3

2. This is one of the cult game now, a lot of people enjoy playing them . Also you can refer to the game :
animal jam 2 | five nights at freddys 2 | hotmail login

3. Can anyone check this website and let me know how to code like this. They are amazingly done their coding and I wanted to get my website done like this.

4. Thanks for the post. Very informative Article.

CEH Courses in bangalore