I am learning c programming by my own effort and I stumbled upon this linked list and I have no idea how this works.

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "assignment.h"
#include "list.h"
void ex7() {
    node_t *a=Initialize('1');
    node_t *b=NULL;
    PrintList(a);
    InsertFirstList(&a, 'V');
    InsertFirstList(&a, 'M');
    PrintList(a);
    InsertLastList(&a, 'C');
    PrintList(a);
    SplitList(&a, &b, 2);
    PrintList(a);
    PrintList(b);
    DeleteFirstList(&a);
    PrintList(a);
    InsertLastList(&a, 'G');
    DeleteLastList(&b);
    PrintList(b);
    InsertLastList(&b,'0');
    PrintList(b);
    InsertLastList(&b, '1');
    PrintList(b);
    MergeList(&a,&b);
    PrintList(a);
    char target='G';
    printf("Count '%c': %d\n",target, SearchList(&a,target));
    target='1';
    printf("Count '%c': %d\n",target, SearchList(&a,target));
    FreeList(&a);
}
node_t *Initialize(char ch) {
    node_t *head;
    head=(node_t*)calloc(1,sizeof(node_t));
    if(head==NULL){
      fprintf(stderr,"Failed to assign memory!\n");
      exit(-1);
    }
    head->next=NULL;
    head->ch=ch;
    return head;
}
void PrintList(node_t *head) {
    node_t *temp=head;
    printf("***Print Linked List***\n");
    while(temp!=NULL) {
      printf("%c ",temp->ch);
      temp=temp->next;
    }
    printf("\n****Print Finished****\n\n");
}
void FreeList(node_t **head) {
    node_t *tmp=NULL;
    node_t *pHead=*head;
    while(pHead->next!=NULL) {
      tmp=pHead;
      pHead=pHead->next;
      free(tmp);
    }
    free(pHead);
}
bool IsEmptyList(node_t *head){
    bool IsEmpty;
    IsEmpty= head==NULL ? false : true;
    return IsEmpty;
}
void InsertFirstList(node_t **head,char insert_char){
    node_t *newHead;
    node_t *temp=*head;
    newHead=(node_t *)calloc(1,sizeof(node_t));
    if(newHead==NULL){
      fprintf(stderr,"Failed to assign memory!\n");
      exit(-1);
    }
    *head=newHead;
    newHead->next=temp;
    newHead->ch=insert_char;
}
void InsertLastList(node_t **head,char insert_char){
    node_t *newTail;
    newTail=(node_t *)calloc(1,sizeof(node_t));
    if(newTail==NULL){
      fprintf(stderr,"Failed to assign memory!\n");
      exit(-1);
    }
    node_t *temp=*head;
    while(temp->next!=NULL){
        temp=temp->next;
    }
    newTail->next=NULL;
    temp->next = newTail;
    newTail->ch=insert_char;
}
void DeleteFirstList(node_t **head){
    node_t *temp=*head;
    *head=(*head)->next;
    free(temp);
}
void DeleteLastList(node_t **head){
    node_t *temp=*head;
    while((temp->next)->next!=NULL){//get to the second last one
        temp=temp->next;
    }
    free((temp->next)->next);
    temp->next=NULL;
}
int SizeList(node_t *head){
    node_t *temp=head;
    int length=0;
    while(temp->next!=NULL){
        temp=temp->next;
      length++;
    }
    return length;
}
int SearchList(node_t **head,char target){
    node_t *temp=*head;
    int time=0;
    while(temp->next!=NULL){
      temp=temp->next;
      if(temp->ch==target) time++;
    }
    return time;
}
void SplitList(node_t **head,node_t **tail, int pos){
    node_t *temp=*head;
    for(int i=0;i<pos-1;i++){
      temp=temp->next;
    }
    *tail=temp->next;
    temp->next=NULL;
}
void MergeList(node_t **head1,node_t **head2){
    node_t *temp=*head1;
    while(temp->next!=NULL){
      temp=temp->next;
    }
    temp->next=*head2;
}

So I found out these nodes have certain functions to initialize, print the list, free the list and so on. I do not understand what the each nodes means and i dont know how the codes in the nodes(Void Initialize, void PrintList....) work. Can somebody help me out understanding it? (comments in the codes would be very appreciated).
Elliot LeeAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Fabrice LambertFabrice LambertCommented:
Well, the first thing I can say is instead of learning from an already written code, try to implement a linked list by yourself.

Basically, a linked list is composed of structures (node) holding data.
The beginning of the list is a pointer to a node, and each node have a pointer to the next node (if it exist, otherwise it is null).
If the 1st pointer is null, that mean the list is empty.
If a node's next pointer is null, it mean it is the last node.
More details here: http://www.learn-c.org/en/Linked_lists

PS: the code you posted is buggy
If a node's dynamic allocation fail, it exit immediatly, regardless of any previous nodes allocated, leaking memory.
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
sarabandeCommented:
If a node's dynamic allocation fail, it exit immediatly

actually, the chance that a memory allocation of an 8-byte structure fails is zero. i would throw out all error handling regarding memory allocation what would make the code a little bit more readable.

next, i would remove Initialize function because the functioniality pretty well can be handled by the insert functions.

the InsertFirstList takes a pointer to pointer of node_t because the head node was replaced. pointer to pointer is the c way to pass a pointer that could be replaced by the calling function. i would change this function to

node_t * InsertFirstList(node_t * head, char insert_char)
{
    node_t * newHead = (node_t *)calloc(1,sizeof(node_t));
    *head=newHead;
    newHead->next = head;   // no temporary required because we return newHead
    newHead->ch=insert_char;
    return newHead;
}

Open in new window


this code is much more readable than the code you posted. moreover, it also is suitable to replace the Initialize function as you can pass a NULL pointer for head. you have to change the calls in main (or in your case in the ex7 function)

    // first insert creates head
    node_t * head = InsertFirstList(head, '1');
    ...
    // further inserts would change to the new head
    head = InsertFirstList(head, 'V');

Open in new window


note, you would pass head and not &head because it is now an input poiner only.

before changing InsertLastList you should add a little helper function

node_t * GetTailList(node_t * head)
{
      node_t * tail = head;
      // also test that initial tail is not null
      while(tail != NULL && tail->next != NULL)
      {
           tail=tail->next;
      }   
      return tail;   
}

Open in new window


the GetTailList can be called everywhere there is a while loop that steps to end of list (tail).

with this InsertLastList would turn to

node_t * InsertLastList(node_t * head, char insert_char)
{
    node_t * newTail = (node_t *)calloc(1,sizeof(node_t));
    node_t * oldTail   = GetTailList(head);
    newTail->ch=insert_char;
    if (oldTail != NULL)
    {
         newTail->next=NULL;
         oldTail->next = newTail;
    }
    else
    {
         head = newTail;
    }
     return head;   
}

Open in new window


note, we always have to return head which is the begin of the list.  

so the function InsertLastList now also could handle the case where head is NULL but all callers should call the function like

head = InsertLastList(head, 'X');

Open in new window


in list.h you would need to change the prototypes of the functions accordingly (and add prototype for GetTailList).

i would recommend to modify all your list functions and comment each significant statement.

Sara
0
phoffricCommented:
The following two lectures from Stanford will explain pointers and linked lists for you .
Lec.12
https://m.youtube.com/watch?v=K8DjFXkMRbY

Lec.13
https://m.youtube.com/watch?v=C36NAaS6ITM

 If you are already comfortable with pointers, then you can skip to the end of the lecture 12 and continue with lecture 13 .
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Programming

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.