From: Richard Knutsson <ricknu-0@student.ltu.se>
To: nanakos@wired-net.gr
Cc: linux-kernel@vger.kernel.org
Subject: Re: Ordered Sorted List
Date: Sat, 26 Nov 2005 13:15:49 +0100 [thread overview]
Message-ID: <438851F5.80304@student.ltu.se> (raw)
In-Reply-To: <1131.62.1.12.53.1133000719.squirrel@webmail.wired-net.gr>
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
prev parent reply other threads:[~2005-11-26 12:10 UTC|newest]
Thread overview: 3+ messages / expand[flat|nested] mbox.gz Atom feed top
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 message]
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=438851F5.80304@student.ltu.se \
--to=ricknu-0@student.ltu.se \
--cc=linux-kernel@vger.kernel.org \
--cc=nanakos@wired-net.gr \
/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