blog

Home / DeveloperSection / Blogs / Simple way to Learn Dynamic Data Structure in C language

Simple way to Learn Dynamic Data Structure in C language

Abhishek Srivasatava1649 02-Nov-2016

During my college days, the data structure is one of the most difficult subjects, we have to perform push, pop, delete, insert, display etc. operations using linked list which is the most basic of data structure. I remember that, I just mug up the program to clear my exam because I don’t know the basic of data structure. As, I was in electronics field so, it was very difficult for us to deal with programming. Now after joining the Software Company I understand how to make a program related to data structure.


Before going through the actual programming part of the data structure one should know basic of structure and pointer.


Example:
struct Node
{
   int data;
   struct Node *next;
}*head = NULL; 


Concept 1: In the above example what is the head and next means, answer of this question is very easy just think about the pointer. I think you have got the answer, Top and next indicating the pointer of the struct which hold the address of struct. It is the most basic think and most of us don’t think about this part.


Concept 2: (->) Most confusing think during learning was this arrow sign.

Example: newNode->data = value; usually while dealing with structure we write newnode.num=5; etc. But while using link list we are using this arrow sign. In reality it is not struct node, it is the pointer of struct node so it assigned like this only.


Here is the explanation of simple linklist program:

#include<stdio.h>
#include<conio.h>
#include<stdlib.h> // because we need to use Malloc function. 
void insertAtBeginning(int);
void insertAtEnd(int);
void insertBetween(int,int,int);
void display();
void removeBeginning();
void removeEnd();
void removeSpecific(int);
struct Node
{
   int data;
   struct Node *next;
}*head = NULL;
void main()
{
   int choice,value,choice1,loc1,loc2;
   clrscr();
   while(1){
   mainMenu: printf("\n\n********* MENU ************\n1.insertAtBeginning\n2.insertAtEnd\n3. insertBetween\n4. display\n 5.removeBeginning 6.removeEnd 7.removeSpecific .Enter your choice: ");
   scanf("%d",&choice);
   switch(choice)
   {
     
  case 1: printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtBeginning(value);
break;
  case 2:
printf("Enter the value to be insert: ");
scanf("%d",&value);
insertAtEnd(value);
break;
 case 3:    
printf("Enter the value to be insert: ");
scanf("%d",&value); 
 printf("Enter the two values where you wanto insert: ");
scanf("%d%d",&loc1,&loc2);
insertBetween(value,loc1,loc2);
break;
case 4: display();
break;
case 5: removeBeginning();
break;
case 6: removeEnd(value);
break;
case 7:      printf("Enter the value which you wanto delete: ");
scanf("%d",&loc2);
removeSpecific(loc2);
break;
default: printf("\nWrong Input!! Try again!!!\n\n");
goto mainMenu;
}
}

Above part of the program is like the menu card (It is not the actual part of the program, it is only to understand, there is a need of some improvement in the above logic.)

Now we have to define the function. And understand the meaning of each function. Before that just look over the block diagram. 
Simple way to Learn Dynamic Data Structure in C language

void insertAtBeginning(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }
   printf("\nValue Inserted!\n");
}


Explanation of above program:

NewNode is the pointer of struct node.


newNode = (struct Node*)malloc(sizeof(struct Node)); // declaring size of node.

NewNode->data=Value // assigning value to the structure

If we are inserting value in 1st node then we need to give the address value as NULL.

Till now, we didn’t hold the address of the structure *newNode. For this we have defined
head= newNode.

But if data is already present in the list then we have think about the address need to be stored.

So, here is the simple way to define above situation.


if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      newNode->next = head;
      head = newNode;
   }


Similarly, you can think for other function and create your own program. Before creating your own program always try to create a block diagram related to Linklist


void insertAtEnd(int value)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   newNode->next = NULL;
   if(head == NULL)
head = newNode;
   else
   {
      struct Node *temp = head;
      while(temp->next != NULL)
temp = temp->next;
      temp->next = newNode;
   }
   printf("\nInserted!!!\n");
}
void insertBetween(int value, int loc1, int loc2)
{
   struct Node *newNode;
   newNode = (struct Node*)malloc(sizeof(struct Node));
   newNode->data = value;
   if(head == NULL)
   {
      newNode->next = NULL;
      head = newNode;
   }
   else
   {
      struct Node *temp = head;
      while(temp->data != loc1 && temp->data != loc2)
temp = temp->next;
      newNode->next = temp->next;
      temp->next = newNode;
   }
   printf("\nInserted!!!\n");
}
void removeBeginning()
{
   if(head == NULL)
printf("\n\nList is Empty!!!");
   else
   {
      struct Node *temp = head;
      if(head->next == NULL)
      {
head = NULL;
free(temp);
      }
      else
      {
head = temp->next;
free(temp);
printf("\nOne node deleted!!!\n\n");
      }
   }
}
void removeEnd()
{
   if(head == NULL)
   {
      printf("\nList is Empty!!!\n");
   }
   else
   {
      struct Node *temp1 = head,*temp2;
      if(head->next == NULL)
head = NULL;
      else
      {
while(temp1->next != NULL)
{
   temp2 = temp1;
   temp1 = temp1->next;
}
temp2->next = NULL;
      }
      free(temp1);
      printf("\n Deleted!!!\n\n");
   }
}
void removeSpecific(int delValue)
{
   struct Node *temp1 = head, *temp2;
   while(temp1->data != delValue)
   {
     if(temp1 -> next == NULL){
printf("\nGiven node not found in the list!!!");
goto functionEnd;
     }
     temp2 = temp1;
     temp1 = temp1 -> next;
   }
   temp2 -> next = temp1 -> next;
   free(temp1);
   printf("\nDeleted!!!\n\n");
   functionEnd:
}
void display()
{
   if(head == NULL)
   {
      printf("\nList is Empty\n");
   }
   else
   {
      struct Node *temp = head;
      printf("\n\nList elements are - \n");
      while(temp->next != NULL)
      {
printf("%d --->",temp->data);
temp = temp->next;
      }
      printf("%d --->NULL",temp->data);
   }
} 

Updated 16-Mar-2018

Leave Comment

Comments

Liked By