Showing posts with label Node. Show all posts
Showing posts with label Node. Show all posts

C Program to Defined Node with Example

How to write a C Program to defined Node in C Programming Language ?

Solution:


/* Node is defined as :
typedef struct node
{
    int val;
    struct node* left;
    struct node* right;
    int ht;
} node; */
 
int nht(node * root);
int height (node * root);
int bfactor (node * root);
node * newnode(int val);
node * insert(node * root, int val);
node * balance (node * root);
node * lr(node * root);
node * rr ( node * root);
 
 
node * newnode(int val)
{
        node * temp = (node *) malloc(sizeof(node));
        temp->val = val;
        temp->left = NULL;
        temp->right = NULL;
        temp->ht = 1;
        return temp;
}
 
int height(node * root)
{
        if(root)
                return root->ht;
        else
                return 0;
}
 
int nht(node * root)
{
        int x = height(root->right);
        int y = height(root->left);
        if (x>y)
                return x+1;
        else
                return y+1;
}
int bfactor(node * root)
{
        return height(root->left)-height(root->right);
}
 
node * rr ( node * root )
{
        node * x;
        x = root->left;
        root->left = x->right;
        x->right = root;
        root->ht = nht(root);
        x->ht = nht(x);
        return x;
}
 
node * lr ( node * root )
{
        node * x;
        x = root->right;
        root->right = x->left;
        x->left = root;
        root->ht = nht(root);
        x->ht = nht(x);
        return x;
}
 
node * insert (node * root, int val)
{
        if (!root)
                return newnode(val);
        else if (root->val < val)
                {
                        root->right= insert (root->right, val);
                }      
 
        else
                {
                        root->left= insert (root->left, val);
                }      
 
        root->ht = nht(root);
        int bal = bfactor(root);
        if(bal>1)
        {
                if (bfactor(root->left)>=0)
                        return rr(root);
                else
                {
                        root->left = lr(root->left);
                        return rr (root);
                }
        }
        if(bal<-1)
        {
                if (bfactor(root->right)<=0)
                        return lr(root);
                else
                {
                        root->right = rr(root->right);
                        return lr (root);
                }              
        }
        return root;
}

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;
}

Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list

How to write a C Program in Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list in C Programming Language ?



/*Menu driven program in the creation,display,search, insertion and deletion of a node in the linked list */

#include<stdio.h>
#include<stdlib.h>

struct node* addofll(struct node*); //prototype of the new linked list block creation
void display(struct node*); // prototype to display the elements in the linked list
void search(struct node*); //prototype for searching to check if the element is in the list
struct node* insert(struct node*); //prototype for inserting a node into the linked list
struct node* delete(struct node*); //prototype for deletion of a node from the linked list

/*creating the struct for the block of linked list*/

struct node  //struct node is required for linked list so as to get data and address
{
int data;
struct node* ptr; // struct node type pointer is required so as to hold the pointer of other struct node type for linking
};

int main()
{

struct node* head; //this pointer holds on the starting value of the data
int i,b;
head=NULL;

while(1)
{
printf("\nwhat do you want to do now?\nPress \n1-> to proceed for element processing \n2-> to display the elements \n3-> to search any element in the list \n4->Insert a node into linked list\n5->Delete the node from linked list\n");
scanf("%d",&b);

switch(b)
{
case 1: head=addofll(head);
break;

case 2: display(head);
break;

case 3: search(head);
break;

case 4: head=insert(head);
break;

case 5: head=delete(head);
break;

default: printf("You have entered an invalid option");
}
}
return 0;
}

void search(struct node*head)
{

int ele;
printf("Enter the element to be searched \n");
scanf("%d",&ele);

if(head!=NULL)
{
while (head!=NULL)
{
if(head->data==ele)
{
printf("\nthe element %d is found in the data\n",ele);
head=head->ptr;
}

else
{
while(head != NULL && head->data != ele)
{
head=head->ptr;

}
}

}

printf("\nThe search didnt find any element later\n");
}
else
printf("\nThere are no elements in the list\nPlease insert a new node\n");

}

void display(struct node* head)
{
printf("the data is displayed below\n");
while(head !=NULL)
{
printf("\n%d \t",head->data);
head=head->ptr;
}
}


struct node* addofll(struct node* head)
{
int dat;
printf("Enter data \n");
scanf("%d",&dat);
struct node* newnode;
newnode=(struct node*)malloc(sizeof(struct node));
newnode->data=dat;
newnode->ptr=NULL;

newnode->ptr=head;
return newnode;

}



struct node* insert(struct node* head)
{
int info,num;
struct node* newnode;
struct node* temp;
newnode=(struct node*)malloc(sizeof(struct node));
printf("\nEnter the value to be inserted\n");
scanf("%d",&info);

newnode->data=info;

printf("\nPoint out the location of insertion in terms of data of linked list\n");
scanf("%d",&num);

temp=head;

if(temp == NULL)
{
printf("\nThe list is empty\nThe new node is the first node\n");
newnode->ptr=NULL;
return newnode;
}


while(temp !=NULL && temp->data !=num)
temp=temp->ptr;


if(temp !=NULL && temp->data ==num)
{
printf("\nyou have found the location of the insertion: inserting %d in list\n",info);
newnode->ptr=temp->ptr;
temp->ptr=newnode;
}

printf("\nyou are out of the loop\n");

return head;


}


struct node* delete(struct node* head)
{
struct node *temp,*del;
int num;
printf("\nEnter the lisked list data element to be removed\n");
scanf("%d",&num);
temp=head;

if(temp !=NULL && temp->data == num)
{
printf("\nThe value is detected and this node is getting deleted\n");
head=temp->ptr;
free(temp);
return head;
}
if(temp !=NULL && temp->ptr->data ==num)
{
del=temp->ptr;
temp->ptr=temp->ptr->ptr;
free(del);
return head;
}
while(temp !=NULL && temp-> data != num)
{
if(temp !=NULL && temp->data !=num) {
temp=temp->ptr;
}
else
return head;
}
if(temp == NULL)
{
printf("\nThere are no elements in the list\nCreate a new node first\n");
return 0;
}


}