I am learning C. I have created a program to count frequency of words from a text file.
My struct contains three keys (frequency, word, nextLink).
The thing is I have sorted array of structs using key, but have no idea how to go about this one. Any guidance, links will be great.
I am providing my arrayOfStructs sort code
void sortArray(int array[], int count)
{
int i,j,temp;
for (i = 0; i < count; ++i)
{
for (j = i + 1; j < count; ++j)
{
if (array[i] > array[j])
{
temp = array[i];
array[i] = array[j];
array[j] = temp;
}
}
}
}
Would this be the possible way to sort LinkedList
void sortList(struct Node *head)
{
struct Node *i, *j, *temp;
for (i = head; i != NULL ; i->next)
{
for (j = head->next; j != NULL; j->next)
{
if (head->frequency < head->next->frequency)
{
temp = head;
head = head->next;
head->next = temp;
}
}
}
}
struct Node
{
int frequency;
char word[50];
struct Node *next;
};
Use i = i->next instead of i->next and j = j->next instead of j->next.
void sortList(struct Node *head)
{
struct Node *i, *j, *temp;
for (i = head; i != NULL ; i = i->next) // i = i->next
{
for (j = head->next; j != NULL; j = j->next) // j = j->next
{
if (head->frequency < head->next->frequency)
{
temp = head;
head = head->next;
head->next = temp;
}
}
}
}
Related
I am learning C and wrote a few functions to work with linked lists in C. However, I have a memory leak (I guess because of the reassignment of the node* list in lines 28 and 29) and was looking for a way to solve it.
My code:
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int number;
struct node *next;
}
node;
node *declare(int value);
int count(node *list);
node *lead(node *list, int value);
void trail(node *list, int value);
node *append(node *list, int value, int index);
node *fetch_node(node *list, int index);
int fetch(node *list, int index);
int *list_to_array(node *list);
node *array_to_list(int array[], int range);
void free_list(node *list);
node *delete(node *list, int index);
void print(node *list);
int main(void)
{
node *list = declare(1);
list = lead(list, -1);
trail(list, 2);
trail(list, 44);
list = delete(list, 0);
append(list, 232, 1);
int *array = list_to_array(list);
node *list_new = array_to_list(array, 4);
printf("%d\n", fetch(list_new, 3));
print(list);
free_list(list);
free_list(list_new);
free(array);
}
node *declare(int value)
{
node *n = malloc(sizeof(node));
n->number = value;
n->next = NULL;
return n;
}
int count(node *list)
{
node *tmp = list;
int count = 1;
while (tmp->next != NULL)
{
tmp = tmp->next;
count++;
}
return count;
}
node *lead(node *list, int value)
{
node *n = declare(value);
node *tmp = list;
n->next = tmp;
return n;
}
void trail(node *list, int value)
{
node *n = declare(value);
node *tmp = list;
while (tmp->next != NULL)
{
tmp = tmp->next;
}
tmp->next = n;
}
node *append(node *list, int value, int index)
{
if (index == 0)
{
return lead(list, value);
}
else
{
int range = count(list);
node *n = declare(value);
node *prev = fetch_node(list, index - 1);
node *follow = fetch_node(list, index);
prev->next = n;
n->next = follow;
return list;
}
}
node *fetch_node(node *list, int index)
{
node *tmp = list;
if (index < count(list))
{
for (int i = 0; i < index; i++)
{
tmp = tmp->next;
}
}
else
{
fprintf(stderr, "VALUE IN FETCH OUT OF RANGE, RETURNING POINTER TO LIST\n");
}
return tmp;
}
int fetch(node *list, int index)
{
node *fetched = fetch_node(list, index);
return fetched->number;
}
node *delete(node *list, int index)
{
if (index == 0)
{
node *follow = list->next;
list = list->next;
}
else
{
node *fetched = fetch_node(list, index);
node *prev, *follow;
if (index > 0)
{
prev = fetch_node(list, index - 1);
}
else
{
prev = list;
}
int range = count(list);
if (index != range - 1 && index < range)
{
follow = fetch_node(list, index + 1);
}
else
{
follow = NULL;
}
prev->next = follow;
}
return list;
}
int *list_to_array(node *list)
{
int range = count(list);
int *array = malloc(sizeof(int) * range);
for (int i = 0; i < range; i++)
{
array[i] = fetch(list, i);
}
return array;
}
node *array_to_list(int array[], int range)
{
node *list = declare(array[0]);
for (int i = 1; i < range; i++)
{
trail(list, array[i]);
}
return list;
}
void free_list(node *list)
{
int range = count(list);
node *tmp = list;
node *del;
for (int i = 0; i < range; i++)
{
del = tmp;
tmp = tmp->next;
free(del);
}
}
void print(node *list)
{
int range = count(list);
printf("[");
for (int i = 0; i < range; i++)
{
int value = fetch(list, i);
printf("%d", value);
if (i != range - 1)
{
printf(", ");
}
}
printf("]\n");
}
And this is Valgrind output:
==18184== 16 bytes in 1 blocks are definitely lost in loss record 1 of 1
==18184== at 0x4C31B0F: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==18184== by 0x4007C4: declare (linked_lists.c:45)
==18184== by 0x400806: lead (linked_lists.c:65)
==18184== by 0x4006F3: main (linked_lists.c:29)
Any ideas?
Your delete function is creating memory leak because it doesn't free the deleted node.
Add code to free that.
node *delete(node *list, int index)
{
if (index == 0)
{
node *follow = list->next;
// list = list->next;
free(list); // free deleted node
list = follow;
}
else
{
node *fetched = fetch_node(list, index);
node *prev, *follow;
if (index > 0)
{
prev = fetch_node(list, index - 1);
}
else
{
prev = list;
}
int range = count(list);
if (index != range - 1 && index < range)
{
follow = fetch_node(list, index + 1);
}
else
{
follow = NULL;
}
free(prev->next); // free deleted node
prev->next = follow;
}
return list;
}
How can i change code here to work with float values in the array, when I'm trying to compile the code, I got an error
so what I need here is my code can work with float values not just int, If i added an array with int values it works fine but with float values it gives me an error
How can i change code here to work with float values in the array, when I'm trying to compile the code, I got an error
so what I need here is my code can work with float values not just int, If i added an array with int values it works fine but with float values it gives me an error
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 100 // Array size
#define NBUCKET 100 // Number of buckets
#define INTERVAL 100 // Each bucket capacity
struct Node {
int data;
struct Node *next;
};
void BucketSort(int arr[]);
struct Node *InsertionSort(struct Node *list);
void print(int arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(int value);
// Sorting function
void BucketSort(int arr[]) {
int i, j;
struct Node **buckets;
// Create buckets and allocate memory size
buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);
// Initialize empty buckets
for (i = 0; i < NBUCKET; ++i) {
buckets[i] = NULL;
}
// Fill the buckets with respective elements
for (i = 0; i < NARRAY; ++i) {
struct Node *current;
int pos = getBucketIndex(arr[i]);
current = (struct Node *)malloc(sizeof(struct Node));
current->data = arr[i];
current->next = buckets[pos];
buckets[pos] = current;
}
// Print the buckets along with their elements
for (i = 0; i < NBUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Sort the elements of each bucket
for (i = 0; i < NBUCKET; ++i) {
buckets[i] = InsertionSort(buckets[i]);
}
printf("-------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < NBUCKET; i++) {
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Put sorted elements on arr
for (j = 0, i = 0; i < NBUCKET; ++i) {
struct Node *node;
node = buckets[i];
while (node) {
arr[j++] = node->data;
node = node->next;
}
}
return;
}
// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list) {
struct Node *k, *nodeList;
if (list == 0 || list->next == 0) {
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0) {
struct Node *ptr;
if (nodeList->data > k->data) {
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for (ptr = nodeList; ptr->next != 0; ptr = ptr->next) {
if (ptr->next->data > k->data)
break;
}
if (ptr->next != 0) {
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = ptr->next;
ptr->next = tmp;
continue;
} else {
ptr->next = k;
k = k->next;
ptr->next->next = 0;
continue;
}
}
return nodeList;
}
int getBucketIndex(int value) {
return value / INTERVAL;
}
void print(int ar[]) {
int i;
for (i = 0; i < NARRAY; ++i) {
printf("%d ", ar[i]);
}
printf("\n");
}
// Print buckets
void printBuckets(struct Node *list) {
struct Node *cur = list;
while (cur) {
printf("%d ", cur->data);
cur = cur->next;
}
}
// Driver code
int main(void) {
int array[NARRAY] = {0.50, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};
printf("Initial array: ");
print(array);
printf("-------------\n");
BucketSort(array);
printf("-------------\n");
printf("Sorted array: ");
print(array);
return 0;
}
Use this code. I've made my own datatype iORf. Use typedef int iORf and typedef float iORf for int and float respectively. You have to switch manually for this.
#include <stdio.h>
#include <stdlib.h>
#define NARRAY 100 // Array size
#define NBUCKET 100 // Number of buckets
#define INTERVAL 100 // Each bucket capacity
typedef int iORf; //float or int, currently int
struct Node
{
iORf data;
struct Node *next;
};
void BucketSort(iORf arr[]);
struct Node *InsertionSort(struct Node *list);
void print(iORf arr[]);
void printBuckets(struct Node *list);
int getBucketIndex(iORf value);
// Sorting function
void BucketSort(iORf arr[])
{
int i, j;
struct Node **buckets;
// Create buckets and allocate memory size
buckets = (struct Node **)malloc(sizeof(struct Node *) * NBUCKET);
// Initialize empty buckets
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = NULL;
}
// Fill the buckets with respective elements
for (i = 0; i < NARRAY; ++i)
{
struct Node *current;
int pos = getBucketIndex(arr[i]);
current = (struct Node *)malloc(sizeof(struct Node));
current->data = arr[i];
current->next = buckets[pos];
buckets[pos] = current;
}
// Print the buckets along with their elements
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Sort the elements of each bucket
for (i = 0; i < NBUCKET; ++i)
{
buckets[i] = InsertionSort(buckets[i]);
}
printf("-------------\n");
printf("Bucktets after sorting\n");
for (i = 0; i < NBUCKET; i++)
{
printf("Bucket[%d]: ", i);
printBuckets(buckets[i]);
printf("\n");
}
// Put sorted elements on arr
for (j = 0, i = 0; i < NBUCKET; ++i)
{
struct Node *node;
node = buckets[i];
while (node)
{
arr[j++] = node->data;
node = node->next;
}
}
return;
}
// Function to sort the elements of each bucket
struct Node *InsertionSort(struct Node *list)
{
struct Node *k, *nodeList;
if (list == 0 || list->next == 0)
{
return list;
}
nodeList = list;
k = list->next;
nodeList->next = 0;
while (k != 0)
{
struct Node *ptr;
if (nodeList->data > k->data)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = nodeList;
nodeList = tmp;
continue;
}
for (ptr = nodeList; ptr->next != 0; ptr = ptr->next)
{
if (ptr->next->data > k->data)
break;
}
if (ptr->next != 0)
{
struct Node *tmp;
tmp = k;
k = k->next;
tmp->next = ptr->next;
ptr->next = tmp;
continue;
}
else
{
ptr->next = k;
k = k->next;
ptr->next->next = 0;
continue;
}
}
return nodeList;
}
int getBucketIndex(iORf value)
{
return (int)value / INTERVAL;
}
void print(iORf ar[])
{
int i;
int flag = 0;
iORf dummy = 1.5;
if (dummy > 1)
flag++;
for (i = 0; i < NARRAY; ++i)
{
if (flag > 0)
printf("%f ", ar[i]);
else
printf("%d ", ar[i]);
}
printf("\n");
}
// Print buckets
void printBuckets(struct Node *list)
{
struct Node *cur = list;
while (cur)
{
printf("%d ", cur->data);
cur = cur->next;
}
}
// Driver code
int main(void)
{
iORf array[NARRAY] = {0.5, 100.00, 99.97, 51.20, 53.90, 28.10, 25.50, 66.40, 65.70, 0.00};
printf("Initial array: ");
print(array);
printf("-------------\n");
BucketSort(array);
printf("-------------\n");
printf("Sorted array: ");
print(array);
return 0;
}
I am trying to write a sorting function for a list.
I don't know why the method 2 can do the sorting but method 1 doesn't.
I think in method 1 I advance the pointers. And other functions I did are correct.
typedef struct Node {
int data;
struct Node * next;
}NODE, *PNODE;
void sort_list(PNODE phead) {
int len = length_list(phead);
int temp, i, j;
//method1
PNODE p = phead->next;
PNODE p1 = p->next;
for (i = 0; i < len - 1; i++) {
for (j = i + 1; j < len; j++) {
if ( p->data > p1->data) {
temp = p->data;
p->data = p1->data;
p1->data = temp;
}
p1 = p1->next;
}
p = p->next;
}
//method 2
/*
PNODE p;
PNODE p1;
for (i = 0, p = phead->next; i < len - 1; i++,p=p->next) {
for (j = i + 1,p1=p->next; j < len; j++,p1=p1->next ) {
if (p->data > p1->data) {
temp = p->data;
p->data = p1->data;
p1->data = temp;
}
}
}*/
Below is the create list and print function that you can run directly.
int main() {
PNODE phead = NULL;
phead = create_list();
sort_list(phead);
traverse_list(phead);
return 0;
}
PNODE create_list(void) {
int len;
int i = 0;
int val;
printf("input the list length: ");
scanf_s("%d", &len);
PNODE phead = (PNODE)malloc(sizeof(NODE));
PNODE ptail = phead;
ptail->next = NULL;
for (; i < len; i++) {
printf("input the data of the node: ");
scanf_s("%d", &val);
PNODE pNew = (PNODE) malloc(sizeof(NODE));
if (pNew == NULL) {
printf("no memory");
exit(-1);
}
pNew->data = val;
pNew->next = NULL; // create a new node
ptail->next = pNew;
ptail = pNew; // ptail record the previous node address.
}
return phead;
}
void traverse_list(PNODE phead) {
PNODE p = phead->next;
while (p != NULL) {
printf("%d ", p->data);
p = p->next;
}
return;
}
int length_list(PNODE phead) {
PNODE p = phead->next;
int len = 0;
while (p!= NULL) {
len++;
p = p->next;
}
return len;
}
Do I miss something in method 1?
You forgot to initialize p1 before the second (and later) runs of inner loop in method 1.
//method1
PNODE p = phead->next;
// move this
// PNODE p1 = p->next;
for (i = 0; i < len - 1; i++) {
PNODE p1 = p->next; // here
for (j = i + 1; j < len; j++) {
if ( p->data > p1->data) {
temp = p->data;
p->data = p1->data;
p1->data = temp;
}
p1 = p1->next;
}
p = p->next;
}
I am trying to code very basic sorting method for linked lists. I am getting unhandled exception. What is the mistake i am making? Here is my code:-
struct LinkedNode// structure for linked list
{
int data;
struct LinkedNode *next;
}*start = NULL;
following function creates a linked list
void CreateLinkedList()
{
LinkedNode *newNode, *current;
printf("enter 5 numbers to create linked list\n");
for(int i=0; i<5; i++)
{
newNode = (struct LinkedNode *)malloc(sizeof(LinkedNode));
scanf("%d", &newNode->data);
newNode->next = NULL;
if(start == NULL)
{
start = newNode;
current = newNode;
}
else
{
current->next = newNode;
current = newNode;
}
}
}
following function is used for sorting the linked list nodes
void SortLinkedList()
{
struct LinkedNode *node=NULL, *temp = NULL;
int tempvar;//temp variable to store node data
node = start;
temp = node->next;//temp node to hold node data and next link
while(node != NULL && node->next != NULL)
{
for(int j=0; j<5; j++)//value 5 because I am taking only 5 nodes
{
if(node->data > temp->data)//swap node data
{
tempvar = node->data;
node->data = temp->data;
temp->data = tempvar;
}
temp = temp->next;
}
node = node->next;
}
}
Try This code
void SortLinkedList()
{
struct LinkedNode *node=NULL, *temp = NULL;
int tempvar;//temp variable to store node data
node = start;
//temp = node;//temp node to hold node data and next link
while(node != NULL)
{
temp=node;
while (temp->next !=NULL)//travel till the second last element
{
if(temp->data > temp->next->data)// compare the data of the nodes
{
tempvar = temp->data;
temp->data = temp->next->data;// swap the data
temp->next->data = tempvar;
}
temp = temp->next; // move to the next element
}
node = node->next; // move to the next node
}
}
1 - outer while loop is use for the total number of pass that will require to sort the linked list..
2- In second while loop we are actually comparing the data of the nodes that we want to sort
Instead of implementing your own sort, you can just use qsort. Of course, qsort requires an array, but that is easy enough to create.
In this case, you know that the list has 5 members. But, if you didn't know, you could just count them.
int size_of_list = 0;
struct LinkedNode *p;
for (p = start; p != NULL; p = p->next) ++size_of_list;
if (size_of_list == 0) {
// Nothing to do
return;
}
Now you can create your array;
struct LinkedNode *arr[size_of_list + 1], **arrp = arr;
for (p = start; p != NULL; p = p->next) *arrp++ = p;
*arrp = NULL;
Then, use qsort on arr. There are lots of examples, but the trick is writing an appropriate comparison function.
int cmp_LinkedNode(const void *a, const void *b) {
const struct LinkedNode * const *aa = a;
const struct LinkedNode * const *bb = b;
return ((*aa)->data > (*bb)->data) - ((*aa)->data < (*bb)->data);
}
//...
qsort(arr, size_of_list, sizeof(*arr), cmp_LinkedNode);
And then, rewire the list into the sorted order.
for (int i = 0; i < size_of_list; ++i) arr[i]->next = arr[i+1];
start = arr[0];
yeah sorting a linked list using nodes/links is a pretty hard job. Spend hours doing it myself but since i have done it why not help others..
What you need to do is simply find the minimum value in your list. Swap it with the head node and the recur for head->next.
The code for sort is only of 3 to 4 lines if you have FindMin() and Swap() functions made..
here is the complete code for sort(),swap()and findmin().
void sort(node **start)
{
if (((*start)->next == NULL) || (*start == NULL))
{
return;
}
node *min = findmin(*start);
swap(*start, min, start);
sort(&((*start)->next));
}
void swap(node *p1, node *p2, node **start)
{
node *p1pre = NULL;
node *p1curr = *start;
while (p1curr!=p1)
{
p1pre = p1curr;
p1curr = p1curr->next;
}
node *p2pre = NULL;
node *p2curr = *start;
while (p2curr != p2)
{
p2pre = p2curr;
p2curr = p2curr->next;
}
if (p1pre != NULL)
{
p1pre->next = p2curr;
}
else
{
*start = p2curr;
}
if (p2pre != NULL)
{
p2pre->next = p1curr;
}
else
{
*start = p1curr;
}
node *temp = p2curr->next;
p2curr->next = p1curr->next;
p1curr->next = temp;
}
node* findmin(node *start)
{
int flag = 0;
if (start == NULL)
{
cout << "list is empty" << endl;
}
else
{
node *curr = start->next;
node *min = start;
while (curr->next != NULL)
{
if (min->value > curr->value)
{
min = curr;
flag++;
}
curr = curr->next;
}
if ((curr->next == NULL) && (min->value > curr->value))
{
min = curr;
flag++;
}
if (flag > 0)
{
return min;
}
}
}
There is something wrong with my add_vertex
In valgrind I am getting an "Uninitialised value was created by a heap allocation" error
at 0x4C27A2E: malloc (vg_replace_malloc.c:270)
by 0x400892: add_vertex (graph.c:56)
typedef struct {
char *dest_name;
int cost;
} dest_cost;
typedef struct Node {
char *name;
dest_cost *destcost;
int num_dest;
struct Node *next;
} Node;
typedef struct {
int size;
struct Node *node;
} Graph;
INITIALIZE GRAPH
void init_graph(Graph *graph) {
if (graph != NULL)
graph->size = 0;
}
ADD VERTEX
int add_vertex(Graph *graph, const char new_vertex[]) {
Node *next, *temp, *new_node;
int i;
if (has_vertex(*graph, new_vertex))
return 0;
new_node = malloc(sizeof(Node));
new_node->name = malloc(strlen(new_vertex) + 1);
strcpy(new_node->name, new_vertex);
new_node->num_dest = 0;
if (graph->size != 0) {
temp = graph->node;
if (strcmp(temp->name, new_vertex) > 0) {
new_node->next = temp;
graph->node = new_node;
graph->size++;
return 1;
}
else for (i = 1; i < graph->size; i++) {
next = temp->next;
if (strcmp(next->name, new_vertex) > 0) {
new_node->next = next;
temp->next = new_node;
graph->size++;
return 1;
}
}
temp->next = new_node;
graph->size++;
return 1;
}
graph->node = new_node;
graph->size++;
return 1;
}
ADDED: CLEAR GRAPH
void clear_graph(Graph *graph) {
int i;
Node *next, *n = graph->node;
if (graph != NULL) {
for (i = 0; i < graph->size; i++) {
next = n->next;
free(n);
n = next;
}
free(graph);
}
}
ADDED TEST
Graph graph;
const char *vertices_to_add[]= {"koala", "platypus", "snake", "salamander",
"gecko", "frog", "dog", "hedgehog"};
int i;
init_graph(&graph);
for (i= 0; i < sizeof(vertices_to_add) / sizeof(vertices_to_add[0]); i++)
add_vertex(&graph, vertices_to_add[i]);
clear_graph(&graph);