public inbox for linux-kernel@vger.kernel.org
 help / color / mirror / Atom feed
* Ordered Sorted List
@ 2005-11-26 10:25 Nanakos Chrysostomos
  2005-11-26 11:03 ` Matthijs Melchior
  2005-11-26 12:15 ` Richard Knutsson
  0 siblings, 2 replies; 3+ messages in thread
From: Nanakos Chrysostomos @ 2005-11-26 10:25 UTC (permalink / raw)
  To: linux-kernel

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.

-
To unsubscribe from this list: send the line "unsubscribe
linux-c-programming" in
the body of a message to majordomo@vger.kernel.org
More majordomo info at  http://vger.kernel.org/majordomo-info.html



^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ordered Sorted List
  2005-11-26 10:25 Ordered Sorted List Nanakos Chrysostomos
@ 2005-11-26 11:03 ` Matthijs Melchior
  2005-11-26 12:15 ` Richard Knutsson
  1 sibling, 0 replies; 3+ messages in thread
From: Matthijs Melchior @ 2005-11-26 11:03 UTC (permalink / raw)
  To: linux-kernel

Nanakos Chrysostomos wrote:
> Hi ,all.Can someone please explain this source code with an example???
.....
> 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

This is done to move to the next item in the list.
The expression is complicated because 'sp' is a pointer to a pointer to
a structure.

>         }
>         n->next = *sp;
>         *sp = n;
> 
> }
> 
.....

-- 
Matthijs Melchior.


^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: Ordered Sorted List
  2005-11-26 10:25 Ordered Sorted List Nanakos Chrysostomos
  2005-11-26 11:03 ` Matthijs Melchior
@ 2005-11-26 12:15 ` Richard Knutsson
  1 sibling, 0 replies; 3+ messages in thread
From: Richard Knutsson @ 2005-11-26 12:15 UTC (permalink / raw)
  To: nanakos; +Cc: linux-kernel

Nanakos Chrysostomos wrote:

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

>***********************************************************************
>#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
>        }
>  
>
This is a _sorted_ list, right. So somehow we have to sort it.
What it does is to either find a node with a value higher then 't', or 
it reach the end of the list. Meanwhile, it just continue walking the list.

>        n->next = *sp;
>        *sp = n;
>  
>
... so here we hock the rest of the list with the new node.

>}
>
>
>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()
>{
>
>  
>
NULL

>      slistInsert(&empty,4);
>  
>
4->NULL

>      slistInsert(&empty,8);
>  
>
4->8->NULL

>      slistInsert(&empty,24);
>
4->8->24->NULL

>      slistInsert(&empty,50);
>  
>
4->8->24->50->NULL

>      slistInsert(&empty,20);
>  
>
4->8->20->24->50->NULL

>      slistInsert(&empty,2);
>  
>
2->4->8->20->24->50->NULL

>      slistRemove(&empty,4);
>  
>
Remove first value equal or more than 4.
2->8->20->24->50->NULL

>      slistInsert(&empty,18);
>  
>
2->8->18->20->24->50->NULL

>      slistPrint(empty);
>  
>
2
8
18
20
24
50

>}
>
>Thank you very much in advance.
>  
>
cu
/Richard Knutsson

^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2005-11-26 20:02 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2005-11-26 10:25 Ordered Sorted List Nanakos Chrysostomos
2005-11-26 11:03 ` Matthijs Melchior
2005-11-26 12:15 ` Richard Knutsson

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox