Data structures in 'C'

                             (Page : 1)

Basic data structures in 'C' are :

1. LINKED LIST 
2. STACK
3. QUEUE


1. LINKED LIST

There are four types of linked list:-
1. Singly linked list
2. Doubly linked list
3. Circular linked list
4. double ended circular linked list

1.1 Singly linked list :

          In This type of linked list, each node contains two fields i.e. data and a link pointing to the next node in the list.



The first node in the list is pointed by a start pointer. The node in the list has a link pointer field containing a Null.

1.2 Doubly Linked List :

           In this type of linked list, each node contains and two links, one link pointing to the previous node and one link pointing to the next node. Doubly linked list can be traversed forward as well as well as backward.




1.3 Circular Linked List :

          Circular linked list are the linked lists which are obtained by linking the last node of the linked list to the first node of the list. In circular linked list, the last node does not contain the NULL pointer. Instead it contains the pointer of the first node.




Program on singly linked list >>

#include<stdio.h>
#include<conio.h>
#include<malloc.h>

void insertatbeg();
void insertatend();
void deletefrombeg();
void deletefromend();

  struct node
  {
   struct node *prev;
   int data;
   struct node *next;
  };
 typedef struct node NODE;
 NODE *start,*tail,*temp,*ptr;

 void insertatbeg()
 {
   NODE *ptr;
   ptr=(NODE*)malloc(sizeof(NODE));
   printf("enter the item\n");
   scanf("%d",&ptr->data);
   if(start==NULL)
   {
    start=tail=ptr;
    ptr->next=ptr->prev=NULL;
   }
   else
   {
     ptr->prev=NULL;
     start->prev=ptr;
     ptr->next=start;
     start=ptr;
   }
  }
 void insertatend()
 {
  NODE *ptr;
  ptr=(NODE*)malloc(sizeof(NODE));
  printf("enter the item\n");
  scanf("%d",&ptr->data);
  if(tail==NULL)
  {
    tail=start=ptr;
    ptr->next=ptr->prev=ptr;
  }
  else
  {
     ptr->next=NULL;
     tail->next=ptr;
     ptr->prev=tail;
     tail=ptr;
  }
 }

 void deletefrombeg()
 {
  NODE *ptr;
  if(start==NULL)
  {
   printf("deletion is not possible\n");
  }
  if(start==tail) 
  {
   ptr=start;
   start=tail=NULL;
  }
  else
  {
   ptr=start;
   start->next->prev=NULL;
   start=start->next;
   free(ptr);
  }
 }

 void deletefromend()
 {
  NODE *ptr;
  if(tail==NULL)
  {
   printf("deletion is not possible\n");
  }
  if(tail==start)
  {
   ptr=tail;
   start=tail=NULL;
  }
  else
  {
   ptr=tail;
   tail->prev->next=NULL;
   tail=tail->prev;
   free(ptr);
  }
 }

void traversalfrombeg()
{
 NODE *temp;
 temp=start;
 while(temp!=NULL)
 {
  printf("\t%d",temp->data);
  temp=temp->next;
 }
}
void traversalfromend()
{
 NODE *temp;
 temp=tail;
 while(temp!=NULL)
 {
  printf("\t%d",temp->data);
  temp=temp->prev;
 }
}

void main()
{
  char ch;      int i;
  clrscr();              // make this as a comment if an error is pointed out in this line. (CODE::BLOCKS)
do
{printf("***********************************************************\n");
 printf("1.Insretatbeg");
 printf(" 2.Insretatend");
 printf(" 3.Deleteatbeg");
 printf("4.Deleteatend\n");
 printf(" 5.Traversalfrombeg");
 printf(" 6.Traversalfromend");
 printf(" 7.Exit");
 printf("\n*********************************************************\n");
 printf("enter your choice");
 scanf("%d",&i);
 switch(i)
 {
  case 1:
     insertatbeg();
     break;
  case 2:
     insertatend();
     break;
  case 3:
     deletefrombeg();
     break;
  case 4:
     deletefromend();
     break;
  case 5:
     traversalfrombeg();
     break;
  case 6:
     traversalfromend();
     break;
  case 7:
     return;
 }fflush(stdin);

 printf("do u want to continue");
 scanf("%c",&ch);
}while(ch=='y'||ch=='Y');
  
 getch();

}


               Next Page>>