Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8190271
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:34:00+00:00 2026-06-07T03:34:00+00:00

I could use a little help. I’m trying to sort a queue of structures

  • 0

I could use a little help. I’m trying to sort a queue of structures by year.

This are my structures:

struct element{

        int id;
        int sign;
        int year;
        int month;
        double amount;

        struct element *next;


    };

struct queue{
    struct element *head;
    struct element *tail;
    struct element *heads;
    struct element *temp;
    struct element *temph;

    int size;
};

and this is the function i wrote:

void sort(struct queue* queue){

if (queue->size == 0){
        printf("Struct is empty\n");}
else {

        struct element* head=queue->head;
        struct element* heads=queue->heads;
        struct element* temp=NULL;
        struct element* temph=queue->head;
        int i, size=queue->size;        

        for(i=0;i<size-1;i++){
        heads=head->next;
            if((head->year)>(heads->year)){

                temp=head;
                head=heads;
                heads=temp;         
            }


        head=head->next;
        heads=NULL;
        temp=NULL;
        }

head=temph;
}

}

It breaks when I copmare: if((head->year)>(heads->year)).
I’m pretty sure that my problem is caused by improper reference to the structure next to head (I named it heads).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-07T03:34:02+00:00Added an answer on June 7, 2026 at 3:34 am

    I omitted all the non-important stuff, and reduced the linked-list bubbele sort to this skeletton.

    void sort(struct queue* queue)
    {
    struct element **pp, *this;
    
        if (!queue->head ){
            fprintf(stderr, "OMG Struct is empty\n");
            return;
           }
    
            for(pp = &queue->head; this = *pp; pp = &(*pp)->next){
            struct element *other = this->next;
                if (!this->next) break;
                if (this->year < other->year) continue;
                /* 
                ** Now, Swap this (b) and other (c) 
                ** old situation: @a -> (b) -> (c) -> (d)
                ** new situation: @a -> (c) -> (b) -> (d) 
                */
                *pp = other;              /* @a  -> (c) */
                this->next = other->next; /* (b) -> (d) */
                other->next = this;       /* (c) -> (b) */
            }
    /* Note: when we get here, "this" will contain the last non-NULL node in the
    ** chain, and can be used to reset the tail-pointer
    */
    return;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I could use a little help. I got this program to work right then
I could use a little help trouble shooting this problem. When using the app
I'am new to Python 3 and could really use a little help. I have
I could use a bit of help with this, as i am new to
I could use a little help in AppEngine land... Using the [Python] API I
I could use a little help with an XPath-Expression. check the code below; how
I'm having some trouble writing a jQuery function and could use a little help.
I am new to XHTML and I could use a little bit of help.
I'm writing my first rails plugin and could use a little help. In a
I'm getting started with Erlang, and could use a little help understanding the different

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.