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 :
Linked List
- C Program to Demonstrates a linked list for numbers.
- C Program to Implementation of List ADT as linked-list
- Pre Order, Post order, In order Implement Binary Tree using linked list
- C Program Simple linked list
- Program to Add Two Polynomials Using Linked List C Program
- Linked List For Getting Employee Details, Display and Search For Salary C Program
- Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list
Create
- How To Write a C program that creates customers' bills for a carpet company when the following is given ?
- C Program to Create VIRUS
- Creates new main.c with empty main boilerplate template
- C Program to create a solution to the Towers of Hanoi game
- C Program to Create a copy of an image
- Ardunio: Create a crystal ball to tell you the future C Program
- C Program to Creating pico device
- Add x and y coordinates to create a new struct in C Program
- C Program To Create Two Singly Linked List and Perform Following Operation
- Create Two Singly Linked List Perform Differences Display It C Program
- C Program Implement Binary Search Tree And Its Traversals
- C Program to Create, Display, Insert and Deletion of Queue Elements
- Creation and Display of Elements in Both Forward and Reverse Direction in a Doubly Linked List
- Calculator Using IF-ELSE in C Program
Node
Fixed Values
Element List