Ok I have wrote some code but Im having a hard time understanding whats wrong. Welcome to write your own and help me out. The grades are what to be sorted.
Ok the structure for this node is:
struct node{
double grade;
char profName[20];
int count;
struct node *next;
};
struct node *insert(struct node *head, struct node *newNode)
{
struct node *temp, *temp2, *newGrade;
newGrade = temp = NULL;
if(head == NULL)
{
head = newNode;
head-%26gt;next = NULL;
}
else if (head-%26gt;next == NULL)
{
if(head-%26gt;grade %26gt; newNode-%26gt;grade)
head-%26gt;next = newNode;
else{
newNode-%26gt;next = head;
head = newNode;
}
}
else
{
temp = head-%26gt;next;
temp2 = head;
/* think the issue is here */
while(temp-%26gt;grade %26lt; newNode-%26gt;grade %26amp;%26amp; temp != NULL)
{
temp = temp-%26gt;next;
temp2 = temp2-%26gt;next;
}
newNode-%26gt;next = temp;
temp2-%26gt;next = newNode;
}
return(head);
}
C program function to that will insert a node into a sorted linked list?
[UPDATED]
// I am assuming the linked list is sorted in ascending order
struct node *insert(struct node *head, struct node *newNode)
{
struct node* temp = head;
struct node* temp2 = head;
if(head == NULL)
{
newNode-%26gt;next = NULL:
head = newNode;
}
else if(head-%26gt;grade %26gt; newNode-%26gt;grade)
{
head = newNode;
newNode-%26gt;next = temp;
}
else
{
while(temp2 != NULL)
{
temp2 = temp-%26gt;next;
if(temp2 == NULL)
{
newNode-%26gt;next = NULL;
temp-%26gt;next = newNode;
}
if(temp2-%26gt;grade %26gt; newNode-%26gt;grade)
{
temp-%26gt;next = newNode;
newNode-%26gt;next = temp2;
temp2 = NULL;
}
else
{
temp = temp2;
}
}
}
return head;
}
Subscribe to:
Post Comments (Atom)
No comments:
Post a Comment