C program allocates new nodes and creates a four element list with fixed values

The program allocates new nodes and creates a four element list with fixed values


Solution:
/* File: linkedL.c. The program allocates new nodes and creates a four element list with fixed values */

#include <stdio.h>
#include <stdlib.h>
#define num_nodes 5

typedef struct node_type *list_type;
typedef struct node_type
{ int data;
  list_type next;
}node_type;
          //prototypes

list_type insertNumber (int num, list_type head);  // Ex1
list_type deleteNumber (int num, list_type head) ; // Ex2
list_type moveFirst (list_type head);              // Ex4
list_type reverse (list_type head);                // Ex5
list_type  GetNewNode (int newInfo);
list_type CreateTestList();
void displayList (list_type list);

void main()
{
list_type list=NULL;
list=CreateTestList();

displayList (list);
list = reverse (list);
printf("\n---------------------------\nthe list after reverse:\n---------------------------\n");
displayList (list);
// Free nodes
fflush(stdin);
getchar();
}

/* Ex1 */
list_type insertNumber (int num, list_type head)
{
list_type link=head, new_record = GetNewNode(num) , temp;
int flag=1;

   if (head==NULL) // if the list is empty
     {
head=new_record;
return head;
     }
   if (num < link->data){
new_record->next=link;
return new_record;
   }
while ( ( link->next != NULL) && (flag) )
{
temp=link->next;       // temp pointing to the next value
if (num <= temp->data)
{
           link->next = new_record;
           new_record->next = temp;
  flag=0;            
}
link = link->next;
}                     // end of while
if (flag) // if not intellized
  link->next = new_record;
return head;
}


/* Ex2 */
list_type deleteNumber (int num, list_type head)
{
list_type link=head, prev;
int flag=1;

if  (head == NULL)        // if the list empty
           return NULL;
while (link->data == num) // ensure that first value isn't num
{
if (link->next != NULL)
 {
prev=link->next;  
free(link);
link=prev;
        }
else                // if there is only one value and he is num
 {
free(link);
return NULL;
 }
      }
head=link;
prev=link;
link=link->next;
while (link!=NULL)
 {
if (link->data == num){
if (link->next != NULL){
prev->next=link->next;
free(link);
link=prev->next;    }
else  // if the value is last and equall to num
 { free(link);
prev->next=NULL;
return head;
 }
}         // end if's
else {
 prev=link;
 link=link->next;
      }
 }
return head;
}

/* Ex4*/
list_type moveFirst (list_type head)
{
list_type link=head, first=head;

if ((!head)||(link->next == NULL)) return head; // if the list is empty or only 1 value
head=head->next;
for (link=head ; link->next != NULL ; link=link->next);
link->next=first;
first->next=NULL;
return head;
}


/* Ex5 */
list_type reverse (list_type head)
{
list_type link=head , last=link, current, NEXT;
int flag=1;

if ((!head) || (head->next == NULL))
return head;
while (link != NULL && flag==1)
 {
current=link;
if (link->next != NULL) {
link=link->next;
current->next = last;
if (link->next != NULL)
{
NEXT=link->next;
link->next=current;
last=link;
link=NEXT;
}
else
{
link->next=current;
flag=0;  
}
}
else // if link->next = NULL
flag=2;
 } // end while
head->next=NULL;
if (flag != 0)
link->next=last;
return link;
}


/* The procedure displayList(list) prints the values of the info fields of the list  */
void displayList (list_type list)
{
int i;
list_type link;

for (link=list, i=0 ; link!=NULL ; i++){
printf("[%d |  ]--->  ",link->data);
    link=link->next;
  }
if (list == NULL)
printf("--------------------\nthe list is empty.\n--------------------");
  else
printf(" NULL");
}


/* The function GetNewNode(newInfo) allocates a new node and assigns newInfo to the info field  */
list_type  GetNewNode (int newInfo)
{
list_type pNode;
pNode = (list_type) malloc (sizeof(*pNode));

if (pNode==NULL){
printf ("error: malloc failed");
}
  else{
pNode->data=newInfo;
    pNode->next=NULL;
  }
return pNode;
}


/* The function CreateTestList() allocates new nodes and creates a four element list with fixed values */

list_type CreateTestList()
{
int infoTable [num_nodes]= {1,2,3,4,5};
list_type list, link, node;
 int i;

/* Create first node and insert into list */
node=GetNewNode (infoTable[0]);
list=node;
/* Create other nodes. Allocation is done by calling GetNewNode() */
for (link=list, i=1 ; i<num_nodes ; i++) {
node=GetNewNode(infoTable[i]);
    link->next=node;
    link=link->next;
  }
  return list;
}


Learn More :