C Programming Lab Manual – 64

Write a C program for Binary Tree Traversal to in-order, Pre-Order, And Post-Order.

Node:
  item
  left
  right

InorderTraversal(node):
  if node is null, return
  InorderTraversal(node.left)
  Print node.item
  InorderTraversal(node.right)

PreorderTraversal(node):
  if node is null, return
  Print node.item
  PreorderTraversal(node.left)
  PreorderTraversal(node.right)

PostorderTraversal(node):
  if node is null, return
  PostorderTraversal(node.left)
  PostorderTraversal(node.right)
  Print node.item

CreateNode(value):
  newNode = new Node
  newNode.item = value
  newNode.left = null
  newNode.right = null
  return newNode

InsertLeft(parent, value):
  parent.left = CreateNode(value)
  return parent.left

InsertRight(parent, value):
  parent.right = CreateNode(value)
  return parent.right

Main:
  root = CreateNode(1)
  leftNode = InsertLeft(root, 12)
  rightNode = InsertRight(root, 9)

  InsertLeft(leftNode, 5)
  InsertRight(leftNode, 6)

  Print "Inorder traversal:"
  InorderTraversal(root)

  Print "\nPreorder traversal:"
  PreorderTraversal(root)

  Print "\nPostorder traversal:"
  PostorderTraversal(root)

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

struct node {
  int item;
  struct node* left;
  struct node* right;
};

// Tree traversal functions
void inorderTraversal(struct node* root) {
  if (root == NULL) return;
  inorderTraversal(root->left);
  printf("%d -> ", root->item);
  inorderTraversal(root->right);
}

void preorderTraversal(struct node* root) {
  if (root == NULL) return;
  printf("%d -> ", root->item);
  preorderTraversal(root->left);
  preorderTraversal(root->right);
}

void postorderTraversal(struct node* root) {
  if (root == NULL) return;
  postorderTraversal(root->left);
  postorderTraversal(root->right);
  printf("%d -> ", root->item);
}

// Create a new Node
struct node* createNode(int value) {
  struct node* newNode = malloc(sizeof(struct node));
  newNode->item = value;
  newNode->left = NULL;
  newNode->right = NULL;
  return newNode;
}

// Insert a node on the left of the parent node
struct node* insertLeft(struct node* parent, int value) {
  parent->left = createNode(value);
  return parent->left;
}

// Insert a node on the right of the parent node
struct node* insertRight(struct node* parent, int value) {
  parent->right = createNode(value);
  return parent->right;
}

int main() {
  //!showMemory() 
  struct node* root = createNode(1);
  struct node* leftNode = insertLeft(root, 12);
  struct node* rightNode = insertRight(root, 9);

  insertLeft(leftNode, 5);
  insertRight(leftNode, 6);

  printf("Inorder traversal:\n");
  inorderTraversal(root);

  printf("\n\nPreorder traversal:\n");
  preorderTraversal(root);

  printf("\n\nPostorder traversal:\n");
  postorderTraversal(root);

  return 0;
}

Inorder traversal:
5 -> 12 -> 6 -> 1 -> 9 ->

Preorder traversal:
1 -> 12 -> 5 -> 6 -> 9 ->

Postorder traversal:
5 -> 6 -> 12 -> 9 -> 1 ->

C Programming Lab Manual – 63

Write a C program for Circular Linked List.

Create a structure definition for a node with two members: an integer 'num' and a pointer to the next node 'nextptr'
Declare a global variable 'stnode' of type 'struct node*' and initialize it to NULL
Define a function ClListcreation that takes an integer parameter 'n':
    Declare local variables: 'i', 'num'
    If 'n' is greater than or equal to 1, then do the following:
        Allocate memory for 'stnode' using 'malloc' and assign it to 'stnode'
        Prompt the user to input data for the first node and store it in 'num'
        Set the 'num' of 'stnode' to the input value
        Set 'nextptr' of 'stnode' to NULL
        Set 'preptr' to 'stnode'
        Iterate from 'i' = 2 to 'n':
            Allocate memory for a new node using 'malloc' and assign it to 'newnode'
            Prompt the user to input data for the 'i'th node and store it in 'num'
            Set the 'num' of 'newnode' to the input value
            Set 'nextptr' of 'newnode' to NULL
            Set 'nextptr' of 'preptr' to 'newnode'
            Set 'preptr' to 'newnode'
        Set 'nextptr' of 'preptr' to 'stnode'

Define a function displayClList:
    Declare a local variable 'tmp' of type 'struct node*'
    Declare a local variable 'n' and initialize it to 1
    If 'stnode' is NULL, then do the following:
        Print "No data found in the List yet."
    Else, do the following:
        Set 'tmp' to 'stnode'
        Print "Data entered in the list are :"
        Do the following:
            Print "Data 'n' = 'tmp->num'"
            Set 'tmp' to 'tmp->nextptr'
            Increment 'n' by 1
        While 'tmp' is not equal to 'stnode'
        
In the main function:
    Declare a local variable 'n'
    Set 'stnode' to NULL
    Print "Circular Linked List : Create and display a circular linked list :"
    Prompt the user to input the number of nodes and store it in 'n'
    Call the function ClListcreation with 'n' as the argument
    Call the function displayClList
    Return 0

Figure: Linked List Operations

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

struct node {
    int num;
    struct node * nextptr;
}*stnode;
 

void ClListcreation(int n);
void displayClList();

int main()
{
    int n;
    stnode = NULL;
	printf("\n\n Circular Linked List : Create and display a circular linked list :\n");
	printf("\n");	   	

    printf(" Input the number of nodes : ");
    scanf("%d", &n);
 
    ClListcreation(n); 
    displayClList();
    return 0;
}

void ClListcreation(int n)
{
    int i, num;
    struct node *preptr, *newnode;

    if(n >= 1)
    {
        stnode = (struct node *)malloc(sizeof(struct node));

        printf(" Input data for node 1 : ");
        scanf("%d", &num);
        stnode->num = num;
        stnode->nextptr = NULL;
        preptr = stnode;
        for(i=2; i<=n; i++)
        {
            newnode = (struct node *)malloc(sizeof(struct node));
            printf(" Input data for node %d : ", i);
            scanf("%d", &num);
            newnode->num = num;
            newnode->nextptr = NULL;	// next address of new node set as NULL
            preptr->nextptr = newnode;	// previous node is linking with new node
            preptr = newnode;   		// previous node is advanced
        }
        preptr->nextptr = stnode; 		//last node is linking with first node
    }
}

void displayClList()
{
    struct node *tmp;
    int n = 1;

    if(stnode == NULL)
    {
        printf(" No data found in the List yet.");
    }
    else
    {
        tmp = stnode;
        printf("\n\n Data entered in the list are :\n");

        do {
            printf(" Data %d = %d\n", n, tmp->num);

            tmp = tmp->nextptr;
            n++;
        }while(tmp != stnode);
    }
}

 Circular Linked List : Create and display a circular linked list :                                           
                                      
 Input the number of nodes : 3                                                                                
 Input data for node 1 : 2                                                                                    
 Input data for node 2 : 5                                                                                    
 Input data for node 3 : 8                                                                                    
                                                                                                              
 Data entered in the list are :                                                                               
 Data 1 = 2                                                                                                   
 Data 2 = 5                                                                                                   
 Data 3 = 8

C Programming Lab Manual – 62

Write a C program for Doubly Linked List.

//Create a new node
allocate memory for newNode
assign the data to newNode.

//Set prev and next pointers of new node
point next of newNode to the first node of the doubly linked list
point prev to null

//Make new node as head node
Point prev of the first node to newNode (now the previous head is the second node)
Point head to newNode

//Insertion in between two nodes
allocate memory for newNode
assign the data to newNode.

//Set the next pointer of new node and previous node
assign the value of next from previous node to the next of newNode
assign the address of newNode to the next of previous node

//Set the prev pointer of new node and the next node
assign the value of prev of next node to the prev of newNode
assign the address of newNode to the prev of next node

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

// node creation
struct Node {
  int data;
  struct Node* next;
  struct Node* prev;
};

// insert node at the front
void insertFront(struct Node** head, int data) {
  // allocate memory for newNode
  struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

  // assign data to newNode
  newNode->data = data;

  // make newNode as a head
  newNode->next = (*head);

  // assign null to prev
  newNode->prev = NULL;

  // previous of head (now head is the second node) is newNode
  if ((*head) != NULL)
    (*head)->prev = newNode;

  // head points to newNode
  (*head) = newNode;
}

// insert a node after a specific node
void insertAfter(struct Node* prev_node, int data) {
  // check if previous node is null
  if (prev_node == NULL) {
    printf("previous node cannot be null");
    return;
  }

  // allocate memory for newNode
  struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

  // assign data to newNode
  newNode->data = data;

  // set next of newNode to next of prev node
  newNode->next = prev_node->next;

  // set next of prev node to newNode
  prev_node->next = newNode;

  // set prev of newNode to the previous node
  newNode->prev = prev_node;

  // set prev of newNode's next to newNode
  if (newNode->next != NULL)
    newNode->next->prev = newNode;
}

// insert a newNode at the end of the list
void insertEnd(struct Node** head, int data) {
  // allocate memory for node
  struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));

  // assign data to newNode
  newNode->data = data;

  // assign null to next of newNode
  newNode->next = NULL;

  // store the head node temporarily (for later use)
  struct Node* temp = *head;

  // if the linked list is empty, make the newNode as head node
  if (*head == NULL) {
    newNode->prev = NULL;
    *head = newNode;
    return;
  }

  // if the linked list is not empty, traverse to the end of the linked list
  while (temp->next != NULL)
    temp = temp->next;

  // now, the last node of the linked list is temp

  // assign next of the last node (temp) to newNode
  temp->next = newNode;

  // assign prev of newNode to temp
  newNode->prev = temp;
}

// delete a node from the doubly linked list
void deleteNode(struct Node** head, struct Node* del_node) {
  // if head or del is null, deletion is not possible
  if (*head == NULL || del_node == NULL)
    return;

  // if del_node is the head node, point the head pointer to the next of del_node
  if (*head == del_node)
    *head = del_node->next;

  // if del_node is not at the last node, point the prev of node next to del_node to the previous of del_node
  if (del_node->next != NULL)
    del_node->next->prev = del_node->prev;

  // if del_node is not the first node, point the next of the previous node to the next node of del_node
  if (del_node->prev != NULL)
    del_node->prev->next = del_node->next;

  // free the memory of del_node
  free(del_node);
}

// print the doubly linked list
void displayList(struct Node* node) {
  struct Node* last;

  while (node != NULL) {
    printf("%d->", node->data);
    last = node;
    node = node->next;
  }
  if (node == NULL)
    printf("NULL\n");
}

int main() {
  // initialize an empty node
  struct Node* head = NULL;

  insertEnd(&head, 5);
  insertFront(&head, 1);
  insertFront(&head, 6);
  insertEnd(&head, 9);

  // insert 11 after head
  insertAfter(head, 11);

  // insert 15 after the seond node
  insertAfter(head->next, 15);

  displayList(head);

  // delete the last node
  deleteNode(&head, head->next->next->next->next->next);

  displayList(head);
}

6->11->15->1->5->9->NULL
6->11->15->1->5->NULL