Translate

Thursday 1 November 2018

Simple Data Structure Implementation


Data Structure Basic Concept and Program using C language

Include the header file 
#include <stdio.h>
#include <stdlib.h>

Define the Node Strucure
struct Node{
 int data;
 struct Node* next;
};

Function to add data on begining
void addBegning(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = *head;
 *head = newNode;
}

Function to add data at last position
void addLast(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* last = *head;
 newNode->data = newData;
 newNode->next = NULL;
 if(*head == NULL){
  *head = newNode;
  return;
 }
 while(last->next != NULL){
  last = last->next;
 }
 last->next = newNode;
 return;
}

Inserting data after some node
void insertAfter(struct Node* head, int newData){ 
 if(head == NULL) return;
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = head->next;
 head->next = newNode;

}

Deleting a element from data structure
void deleteElement(struct Node** head, int element){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* temp = *head, *prev;
 if(temp != NULL && temp->data == element){
  *head = temp->next;
  free(temp);
  return;
 }
 while(temp != NULL && temp->data != element){
  prev = temp;
  temp = temp->next;
 }
 if(temp == NULL){ printf("Element does not exist in list"); return;}
 prev->next = temp->next;
 free(temp);
}

Reverse the Linked List
static void reverseList(struct Node** head){
 struct Node* current = *head;
 struct Node* next = NULL;
 struct Node* prev = NULL;
        while(current != NULL){
              next = current->next;
                current->next = prev;
                prev = current;
                current = next;
        }
 *head = prev;
}

Print the data
void printData(struct Node* node){
 while(node != NULL){
  printf("%d\t", node->data);
  node = node->next;
 }
 printf("\n");
}

Main 
int main(){
 struct Node* head = NULL;
 addBegning(&head, 2);
 addBegning(&head, 50);
 addBegning(&head, 9);
 addLast(&head, 10);
 addLast(&head, 25);
 insertAfter(head->next->next, 30); //head-1, head->next-2, head->next->next-3
 printf("Before Deletion\n");
 printData(head);
 deleteElement(&head, 30);
 printf("After Deletion element 30\n");
 printData(head);
 printf("After Reverse Linked list\n");
 reverseList(&head);
 printData(head);
 return 0;
}

One File Program 
#include <stdio.h>
#include <stdlib.h>

struct Node{
 int data;
 struct Node* next;
};

void addBegning(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = *head;
 *head = newNode;
}
void addLast(struct Node** head, int newData){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* last = *head;
 newNode->data = newData;
 newNode->next = NULL;
 if(*head == NULL){
  *head = newNode;
  return;
 }
 while(last->next != NULL){
  last = last->next;
 }
 last->next = newNode;
 return;
}
void insertAfter(struct Node* head, int newData){ 
 if(head == NULL) return;
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 newNode->data = newData;
 newNode->next = head->next;
 head->next = newNode;

}
void deleteElement(struct Node** head, int element){
 struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
 struct Node* temp = *head, *prev;
 if(temp != NULL && temp->data == element){
  *head = temp->next;
  free(temp);
  return;
 }
 while(temp != NULL && temp->data != element){
  prev = temp;
  temp = temp->next;
 }
 if(temp == NULL){ printf("Element does not exist in list"); return;}
 prev->next = temp->next;
 free(temp);
}
static void reverseList(struct Node** head){
 struct Node* current = *head;
 struct Node* next = NULL;
 struct Node* prev = NULL;
        while(current != NULL){
              next = current->next;
                current->next = prev;
                prev = current;
                current = next;
        }
 *head = prev;
}
void printData(struct Node* node){
 while(node != NULL){
  printf("%d\t", node->data);
  node = node->next;
 }
 printf("\n");
}
int main(){
 struct Node* head = NULL;
 addBegning(&head, 2);
 addBegning(&head, 50);
 addBegning(&head, 9);
 addLast(&head, 10);
 addLast(&head, 25);
 insertAfter(head->next->next, 30); //head-1, head->next-2, head->next->next-3
 printf("Before Deletion\n");
 printData(head);
 deleteElement(&head, 30);
 printf("After Deletion element 30\n");
 printData(head);
 printf("After Reverse Linked list\n");
 reverseList(&head);
 printData(head);
 return 0;
}

Give Something to the world and it will never let you down.  
Onkar Dubey 




No comments:

Post a Comment