linux-c-programming.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: "Nanakos Chrysostomos" <nanakos@wired-net.gr>
To: linux-c-programming@vger.kernel.org
Subject: Sorted List
Date: Sat, 26 Nov 2005 04:28:21 +0200 (EET)	[thread overview]
Message-ID: <1379.62.1.10.54.1132972101.squirrel@webmail.wired-net.gr> (raw)

Hi ,all.Can someone please explain this source code with an example???

***********************************************************************
#include <stdio.h>

typedef struct list_tag {
        int data;
        struct list_tag *next;
}ListNode;

typedef ListNode *slist;
slist empty = NULL;

void slistInsert(slist *sp,int t)
{
        ListNode *n=(ListNode *)malloc(sizeof(ListNode));
        if(n == NULL)
        {
                printf("Out of memory\n");
                exit(1);
        }
        n->data = t;
        while(*sp!=NULL && (*sp)->data < t)
        {
        sp = &((*sp)->next);   //Why we do this here,i miss this point
        }
        n->next = *sp;
        *sp = n;

}


void slistRemove(slist *sp,int t)
{
        ListNode *n;
        while(*sp!=NULL && (*sp)->data <t)
                sp = &((*sp)->next);
        if(*sp == NULL)
        {
                printf("Not found\n");
                exit(1);
        }

        n=*sp;
        *sp = (*sp)->next;
        free(n);
}

void slistPrint(slist s)
{
        ListNode *n;
        for(n=s;n!=NULL; n=n->next)
                printf("%d\n",n->data);
}

void main()
{

      slistInsert(&empty,4);
      slistInsert(&empty,8);

      slistInsert(&empty,24);
      slistInsert(&empty,50);
      slistInsert(&empty,20);
      slistInsert(&empty,2);
      slistRemove(&empty,4);
      slistInsert(&empty,18);
      slistPrint(empty);
}

Thank you very much in advance.


             reply	other threads:[~2005-11-26  2:28 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2005-11-26  2:28 Nanakos Chrysostomos [this message]
  -- strict thread matches above, loose matches on Subject: below --
2005-11-26 12:06 Sorted List Bharat Jain

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1379.62.1.10.54.1132972101.squirrel@webmail.wired-net.gr \
    --to=nanakos@wired-net.gr \
    --cc=linux-c-programming@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).