Need some help creating a bubble sort for my linked list in C. Here are the structs used in my code:
struct simulation
{
void *list;
int et; /* in seconds */
};
struct plane
{
double x, y, altitude;
char callsign[15];
struct simulation *sim;
};
Here is the struct used in my linked list:
struct Node
{
void *data;
struct Node *next;
};
Here is my insert function:
//ComparisonFunction and FILE not critical to the function at the
moment.
int insert (void *p2head, void *data, ComparisonFunction
goesInFrontOf, FILE *diagnostics)
{
struct Node *newNode = (struct Node*)
malloc(sizeof(struct Node));
struct Node **head_ref = (struct Node** )p2head;
if (newNode == NULL)
{
printf("ERROR!\n");
return 0;
}
newNode->data = data;
if(newNode != NULL &&
goesInFrontOf(newNode->data, data))
{
newNode->next = *head_ref;
(*head_ref) = newNode;
}
return 1;
}
Insert is called like so in a function:
void add_plane_to_sim(struct plane *fake, struct simulation
*sim)
{
struct plane *real;
if( real = allocate_plane(sim->diagnostics))
//allocate_plane just allocates a struct using calloc and
statically counts
//successful allocations. allocate_plane returns a struct.
{
/* copy the data and toss on
the list */
*real = *fake;
insert(&sim->list, real);
}
/* no else - allocate plane already complained
*/
}
Now I need to create a sorting function, which can use the
bubble sort algorithm. Here is the declaration:
/* sort moves data, not nodes, so head won't change
*/
void sort(void *head, ComparisonFunction
goesInFrontOf)
{
....
}
Sort is called like so: sort(sim->list,
westmost);
I'm not too sure how to go about doing this. I don't know how to move data within the nodes so I can sort them by data and not manipulate the nodes. Providing all of my code would be too much as its in 4 different files. The only issue I have is with the sort function. A function pointer is used as the second parameter to sort the data based off of position data. Ive attached the code the ComparisonFunction will call below. The sort function cannot call the plane or sim structure. It can only call the node struct like shown in my insert function. I hope this is enough code to work with. Any help would be great thank you!
int westmost(void *west, void *east) //call for
comparison function
{
struct plane *w, *e;
w = west;
e = east;
return(w->x <= e->x);
}
I need to complete the sort function, but im not sure how. Sort needs to sort the linked list by data. I am passing sim->list to sort, the sorting function cant know the type of structure it is which is why it takes a void pointer. The only structure sort can see is struct Node. sort needs to use the void pointer to sort data within the linked list. A comparison will be made with the second parameter that will determine if the position is out of view (data in another file, not needed since westmost does the calculation). I'm just not sure how to do a bubble sort of data on a void pointer.
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node *next;
};
typedef struct node nd;
nd *start=0;
int count=0;
void insert_at_end(int item)
{
nd *temp,*head;
head=(int*)malloc(sizeof(nd));
head->data=item;
head->next=0;
if(start==0)
start=head;
else
{
temp=start;
while(temp->next!=0)
temp=temp->next;
temp->next=head;
}
count++;
}
void bubble_sort()
{
nd *temp2;
int i,j,temp_int;
for(i=0;i<count-1;i++)
{
temp2=start;
for(j=0;j<count-i-1;j++)
{
if(temp2->data>temp2->next->data)
{
temp_int=temp2->data;
temp2->data=temp2->next->data;
temp2->next->data=temp_int;
}
temp2=temp2->next;
}
}
}
void display()
{
nd *temp=start;
bubble_sort();
while(temp!=0)
{
printf("%d=>",temp->data);
temp=temp->next;
}
printf("NULL");
}
void main()
{
int choice,item;
while(1)
{
printf("\n1.Enter at the end of linked list");
printf("\n2.Display sorted linked list");
printf("\nEnter your choice:");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("Enter a number to insert:");
scanf("%d",&item);
insert_at_end(item);
break;
case 2:
display();
}
}
}

Need some help creating a bubble sort for my linked list in C. Here are the...
Working in C++, Complete the code. // Lab 9 code /* Insert leading comments with your name and the date. Describe the purpose of the code. Also, list each pointer and describe how it is used to enable the selection sort for the linked list structure. */ /* Insert code as described by the comments. */ /* Add comments to each line of code that uses a pointer, describing how it is being used. */ #include <iostream> using namespace std;...
You are given a pointer to a singly linked list. The singly linked list has cycles due to a programming error. Write a C program to detect whether the linked list has cycles without storing all the pointers to the nodes. The function detect_cycles should return 1 when a cycle is found and 0 when there are no cycles. Your code should handle all corner cases carefully and should not cause segmentation fault. Figure shows various examples of cycles for...
Please fill in this code to reverse a linked list: (written in C/C++) #include #include #include using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list */ void...
Please fill in the code to reverse a linked list. IN C++ #include <stdio.h> #include <stdlib.h> #include <iostream> using namespace std; /* Link list node */ struct Node { int data; // your code here }; /* Function to reverse the linked list */ static void reverse(struct Node** head_ref) { // your code here } /* Function to push a node */ void push(struct Node** head_ref, int new_data) { // your code here } /* Function to print linked list...
Using the provided Linked List example code, linkedlist.c, as an example Make a node struct that holds ints instead of strings. In your main, insert 25 to 75 random integers with range (0 to 100) into a linked list of your nodes. Use a random to determine how many to make. Write a function int sum(NodePointer current) that returns the sum of the integers in the list by looping through the linked list. Write a function int count(NodePointer current) that...
implement a doubly-linked list in C. Each node in the linked list should contain a string, a pointer to the previous node (or NULL), and a pointer to the next node (or NULL). The nodes should be sorted by their strings. struct node_t { char* str; struct node_t* prev; struct node_t* next; } To maintain the doubly-linked list, you should keep a pointer to the head node of the list (or NULL if the list is empty), and a pointer...
Programming in C: I am trying to modify this linked list to be doubly linked list. I’m also trying to add a print in reverse function. I’m really struggling with how to change the insert function to doubly link the nodes without effecting the alphabetical sorting mechanism. Example of desired output: Enter your choice: 1 to insert an element into the list. 2 to delete an element from the list. 3 to end. ? 1 Enter a character: a The...
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;...
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...
Extend Linked List in C // Exercise 5 /* Parameter head points to the first node in a linked list, or is * NULL if the list is empty. * * Parameter other points to the first node in a linked list, or is * NULL if the list is empty. * * Extend the linked list pointed to by head so that it contains * copies of the values stored in the linked list pointed to by other. *...