Hi ,all.Can someone please explain this source code with an example???Homework?
***********************************************************************This is a _sorted_ list, right. So somehow we have to sort it.
#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;... so here we hock the rest of the list with the new node.
*sp = n;
}NULL
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);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.
slistInsert(&empty,18);2->8->18->20->24->50->NULL
slistPrint(empty);2
}cu
Thank you very much in advance.