SlideShare a Scribd company logo
/* Write a C program that uses functions to perform the following operations on doubly linked list.:

            i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways

*/



#include "stdio.h"

#include "alloc.h"



typedef struct dubll

{

int data;

struct dubll *leftlink,*rightlink;

}*DUBLL;



DUBLL high,temp_node,low,last,pntr;

int flag=0;



DUBLL NodeAlloc();

DUBLL Search(int,int);



void CreateItem();

void AppendItem();

void PrintItem();

void DeleteItem();

DUBLL Search(int item,int flag);
DUBLL NodeAlloc();

void InsertItem();



void main(void)

{

int choice,Item;

high=NULL;

while(1)

{

    clrscr();

    printf("n ttt***** M A I N M E N U *****nn");

 printf("n 1: Create Linked List n 2: Append a Node to the List n 3: Traverse the List n 4: Delete a
Node from the List n 5: Search a Node n 6: Insert a Node to the List n 7: Close nntt Enter your
Option [ ]bb");

    scanf("%d",&choice);

    switch(choice)

    {

        case 1:

           CreateItem();

           puts("nPress any key to go back to main menu.");

              getch();

           break;

        case 2:

           AppendItem();

           break;
case 3:

       PrintItem();

   puts("nPress any key to go back to main menu.");

   getch();

       break;

case 4:

   DeleteItem();

   break;

case 5:

   printf("Find an Item: ");

   scanf("%d",&Item);

   temp_node=Search(Item,0);

   if(temp_node)

   {

           puts("The item is available in the Linked List.");

   }

   else

       {

   puts("The item is not found in the Linked List.");

   }

   getch();

           break;

case 6:

   InsertItem();
break;

        case 7:

           exit();

        default:

             puts("Invalid choice.");

             puts("nPress any key to go back to main menu.");

                   getch();

             break;

    }

    }

}



/* Function to Create the list*/

void CreateItem()

{

    if(high==NULL)

    {

        printf("n --Creating the list--");

        temp_node=NodeAlloc();

        printf("n Enter starting data (as integer value) :");

        scanf("%d",&temp_node->data);

        high=temp_node;

    }

    else{ printf("n List already created @ %d with %d as data.",high,high->data);}
}



/* Function to Append items to the list*/

void AppendItem()

{

    low=high;

    if(high==NULL)

    {

        CreateItem();

    }

    else

    {

        temp_node=NodeAlloc();

        printf("n Enter Item (in integer) :");

        scanf("%d",&temp_node->data);

        temp_node->rightlink=NULL;



        while(low->rightlink!=NULL)

        low=low->rightlink;

        low->rightlink=temp_node;

        temp_node->leftlink=low;

        last=low->rightlink;



    }
}



/* Function to Traverse the list both ways and print the data*/

void PrintItem()

{

    DUBLL temp_node;

    if(high==NULL)

    {

        printf("n List is not available. Please create a list first.");

        getch();

        CreateItem();

    }

    temp_node=high;

    last=low->rightlink;

    printf("n--Printing The List In Forward direction--n");



    while(temp_node!=NULL)                    //In forward direction

              {

                  printf("t %d",temp_node->data);

                  temp_node = temp_node->rightlink;

              }

    printf("n");

    printf("n--Printing The List In Backward direction--n");

              temp_node=high;
if(temp_node->rightlink==NULL){printf("%d",temp_node->data);return; }

               while(last!=NULL)                 //In backward direction

               {

                   printf("t %d",last->data);

                   last = last->leftlink;

                }

}



/* Function to Delete items of the list*/

void DeleteItem()

{

int value;

DUBLL temp_node;

if(high==NULL)

{

    printf("n List is not available. Please create a list first.");

    getch();

    CreateItem();

}

printf("n Item to delete :");

scanf("%d",&value);

pntr=Search(value,1);

pntr->leftlink->rightlink=pntr->rightlink;

pntr->rightlink->leftlink=pntr->leftlink;
temp_node=pntr;

free(temp_node);

}



/* Function to Search an item from the list*/

DUBLL Search(int item,int flag)

{

    temp_node = high;

    if(high==NULL)

    {

        printf("n List is not available. Please create a list first.");

        getch();

        CreateItem();

    }

    while(temp_node!=NULL)

    {

    if(temp_node->data==item )

    {

        if(flag==0)

        {

            return(1);

        }

        else

        {
return(temp_node);

        }

     }

     temp_node=temp_node->rightlink;

    }

}



/* Function to Allocate nodes*/

DUBLL NodeAlloc()

{

    DUBLL tmep_node;

    tmep_node=malloc(sizeof(struct dubll));

    if(tmep_node==NULL)

        {

         printf("n No memory available. Node allocation cannot be done.");

        }

        tmep_node->rightlink=tmep_node->leftlink=NULL;

    return(tmep_node);

}



/* Function to Insert items in the middle of the list*/

void InsertItem()

{

    int node;
DUBLL temp_node;



    if(high==NULL)

    {

        printf("n List is not available. Please create a list first.");

        getch();

        CreateItem();

    }

    temp_node=NodeAlloc();

    printf("Position At which node to be inserted: ___ & New Item Value: ___ ");

    scanf("%d",&node);

    scanf("%d",&temp_node->data);

    pntr=Search(node,1);



    if(pntr->rightlink==NULL){printf("n The operation is not possible."); getch();return;}

        temp_node->leftlink=pntr;                 //creating link to new node

        temp_node->rightlink=pntr->rightlink;



        pntr->rightlink->leftlink=temp_node;

        pntr->rightlink=temp_node;



        printf("n Item has been Inserted.");

        getch();

}

More Related Content

PPTX
Single linked list
Sayantan Sur
 
PPTX
Circular linked list
Sayantan Sur
 
DOCX
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
DOC
Final ds record
Ganisius Ganish
 
DOC
C program to insert a node in doubly linked list
Sourav Gayen
 
PPTX
Double linked list
Sayantan Sur
 
PDF
Binary search
Hitesh Kumar
 
Single linked list
Sayantan Sur
 
Circular linked list
Sayantan Sur
 
C program to implement linked list using array abstract data type
loyola ICAM college of engineering and technology
 
Final ds record
Ganisius Ganish
 
C program to insert a node in doubly linked list
Sourav Gayen
 
Double linked list
Sayantan Sur
 
Binary search
Hitesh Kumar
 

What's hot (20)

PPTX
Linked list
Kumar
 
PPTX
Stack using Linked List
Sayantan Sur
 
PPTX
Double linked list
raviahuja11
 
PDF
Data structure circular list
iCreateWorld
 
DOCX
Data structure output 1
Balaji Thala
 
PDF
Data Structures Practical File
Harjinder Singh
 
DOCX
One dimensional operation of Array in C- language
9096308941
 
PPTX
Examples sandhiya class'
Dr.Sandhiya Ravi
 
DOCX
Data Structures Using C Practical File
Rahul Chugh
 
PDF
Data Structure using C
Bilal Mirza
 
PPTX
Binary tree
raviahuja11
 
PDF
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
Chad Petrovay
 
DOCX
Linked list imp of list
Elavarasi K
 
DOCX
Pratik Bakane C++
pratikbakane
 
PDF
Git avançado
Jean Carlo Machado
 
PDF
Functional php
Jean Carlo Machado
 
DOCX
DataStructures notes
Lakshmi Sarvani Videla
 
Linked list
Kumar
 
Stack using Linked List
Sayantan Sur
 
Double linked list
raviahuja11
 
Data structure circular list
iCreateWorld
 
Data structure output 1
Balaji Thala
 
Data Structures Practical File
Harjinder Singh
 
One dimensional operation of Array in C- language
9096308941
 
Examples sandhiya class'
Dr.Sandhiya Ravi
 
Data Structures Using C Practical File
Rahul Chugh
 
Data Structure using C
Bilal Mirza
 
Binary tree
raviahuja11
 
Advanced Crystal Reports: Techniques for compiling Annual Reports & other sta...
Chad Petrovay
 
Linked list imp of list
Elavarasi K
 
Pratik Bakane C++
pratikbakane
 
Git avançado
Jean Carlo Machado
 
Functional php
Jean Carlo Machado
 
DataStructures notes
Lakshmi Sarvani Videla
 
Ad

Viewers also liked (6)

PDF
kerala psc ayurveda medial officer 2012
Archana Girijakumari
 
PDF
Ay psc 147 2005
syyed mohammed jalaludeen
 
DOCX
Kerala psc sa previous exam solved papers
Sura Books
 
PPT
BrainFingerprintingpresentation
KITE www.kitecolleges.com
 
PPT
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
KITE www.kitecolleges.com
 
kerala psc ayurveda medial officer 2012
Archana Girijakumari
 
Kerala psc sa previous exam solved papers
Sura Books
 
BrainFingerprintingpresentation
KITE www.kitecolleges.com
 
DISTRIBUTED INTERACTIVE VIRTUAL ENVIRONMENT
KITE www.kitecolleges.com
 
Ad

Similar to week-14x (20)

DOCX
Shortened Linked List in C programming easy to learn for exam
ssuser80a5aa
 
PDF
DATA STRUCTURE USING C & C++
mustkeem khan
 
PDF
DSU C&C++ Practical File Diploma
mustkeem khan
 
DOCX
Data structure and algorithm lab spiral (1) (4) (1).docx
parkavimannar
 
DOCX
Linked lists
George Scott IV
 
PDF
Datastructures asignment
sreekanth3dce
 
PDF
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
PDF
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
PDF
program on string in java Lab file 2 (3-year)
Ankit Gupta
 
PDF
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
PDF
a) Complete both insert and delete methods. If it works correctly 10.pdf
MAYANKBANSAL1981
 
DOCX
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
GordonpACKellyb
 
DOCX
Programs
kulwinderbawa007
 
PDF
My C proggram is having trouble in the switch in main. Also the a co.pdf
meerobertsonheyde608
 
PDF
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
ARYAN20071
 
PDF
The Program to find out the middle of a given linked listclass Lin.pdf
anushkaent7
 
PDF
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
PPTX
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
ankitnegics07
 
PPTX
class 2.pptxdsdhwdhwdbgkjbdqwnddbqsddbbd
ankitnegics07
 
Shortened Linked List in C programming easy to learn for exam
ssuser80a5aa
 
DATA STRUCTURE USING C & C++
mustkeem khan
 
DSU C&C++ Practical File Diploma
mustkeem khan
 
Data structure and algorithm lab spiral (1) (4) (1).docx
parkavimannar
 
Linked lists
George Scott IV
 
Datastructures asignment
sreekanth3dce
 
mainpublic class AssignmentThree {    public static void ma.pdf
fathimafancyjeweller
 
JAVA A double-ended queue is a list that allows the addition and.pdf
amrishinda
 
program on string in java Lab file 2 (3-year)
Ankit Gupta
 
Program of sorting using shell sort #include stdio.h #de.pdf
anujmkt
 
a) Complete both insert and delete methods. If it works correctly 10.pdf
MAYANKBANSAL1981
 
IN C LANGUAGE- I've been trying to finish this program for the last fe.docx
GordonpACKellyb
 
My C proggram is having trouble in the switch in main. Also the a co.pdf
meerobertsonheyde608
 
BINARY SEARCH TREE OPERATIONS #includestdio.h#includestdlib.pdf
ARYAN20071
 
The Program to find out the middle of a given linked listclass Lin.pdf
anushkaent7
 
Note             Given Code modified as required and required met.pdf
Ankitchhabra28
 
class 2.pptxdsafe fnbwfl;d,qgyewbjdkqwd;wd
ankitnegics07
 
class 2.pptxdsdhwdhwdbgkjbdqwnddbqsddbbd
ankitnegics07
 

More from KITE www.kitecolleges.com (20)

Recently uploaded (20)

PDF
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
PPTX
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
PPTX
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
DOCX
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
PDF
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
PPTX
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
PDF
Virat Kohli- the Pride of Indian cricket
kushpar147
 
PPTX
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
PPTX
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
PPTX
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
PPTX
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
PPTX
Virus sequence retrieval from NCBI database
yamunaK13
 
PPTX
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
PPTX
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
PPTX
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
PPTX
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
DOCX
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
DOCX
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
PDF
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
PDF
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 
Health-The-Ultimate-Treasure (1).pdf/8th class science curiosity /samyans edu...
Sandeep Swamy
 
How to Close Subscription in Odoo 18 - Odoo Slides
Celine George
 
CARE OF UNCONSCIOUS PATIENTS .pptx
AneetaSharma15
 
Modul Ajar Deep Learning Bahasa Inggris Kelas 11 Terbaru 2025
wahyurestu63
 
Module 2: Public Health History [Tutorial Slides]
JonathanHallett4
 
Command Palatte in Odoo 18.1 Spreadsheet - Odoo Slides
Celine George
 
Virat Kohli- the Pride of Indian cricket
kushpar147
 
Cleaning Validation Ppt Pharmaceutical validation
Ms. Ashatai Patil
 
HISTORY COLLECTION FOR PSYCHIATRIC PATIENTS.pptx
PoojaSen20
 
Introduction to pediatric nursing in 5th Sem..pptx
AneetaSharma15
 
An introduction to Dialogue writing.pptx
drsiddhantnagine
 
Virus sequence retrieval from NCBI database
yamunaK13
 
CONCEPT OF CHILD CARE. pptx
AneetaSharma15
 
Five Point Someone – Chetan Bhagat | Book Summary & Analysis by Bhupesh Kushwaha
Bhupesh Kushwaha
 
Applications of matrices In Real Life_20250724_091307_0000.pptx
gehlotkrish03
 
family health care settings home visit - unit 6 - chn 1 - gnm 1st year.pptx
Priyanshu Anand
 
Unit 5: Speech-language and swallowing disorders
JELLA VISHNU DURGA PRASAD
 
pgdei-UNIT -V Neurological Disorders & developmental disabilities
JELLA VISHNU DURGA PRASAD
 
What is CFA?? Complete Guide to the Chartered Financial Analyst Program
sp4989653
 
BÀI TẬP TEST BỔ TRỢ THEO TỪNG CHỦ ĐỀ CỦA TỪNG UNIT KÈM BÀI TẬP NGHE - TIẾNG A...
Nguyen Thanh Tu Collection
 

week-14x

  • 1. /* Write a C program that uses functions to perform the following operations on doubly linked list.: i) Creation ii) Insertion iii) Deletion iv) Traversal in both ways */ #include "stdio.h" #include "alloc.h" typedef struct dubll { int data; struct dubll *leftlink,*rightlink; }*DUBLL; DUBLL high,temp_node,low,last,pntr; int flag=0; DUBLL NodeAlloc(); DUBLL Search(int,int); void CreateItem(); void AppendItem(); void PrintItem(); void DeleteItem(); DUBLL Search(int item,int flag);
  • 2. DUBLL NodeAlloc(); void InsertItem(); void main(void) { int choice,Item; high=NULL; while(1) { clrscr(); printf("n ttt***** M A I N M E N U *****nn"); printf("n 1: Create Linked List n 2: Append a Node to the List n 3: Traverse the List n 4: Delete a Node from the List n 5: Search a Node n 6: Insert a Node to the List n 7: Close nntt Enter your Option [ ]bb"); scanf("%d",&choice); switch(choice) { case 1: CreateItem(); puts("nPress any key to go back to main menu."); getch(); break; case 2: AppendItem(); break;
  • 3. case 3: PrintItem(); puts("nPress any key to go back to main menu."); getch(); break; case 4: DeleteItem(); break; case 5: printf("Find an Item: "); scanf("%d",&Item); temp_node=Search(Item,0); if(temp_node) { puts("The item is available in the Linked List."); } else { puts("The item is not found in the Linked List."); } getch(); break; case 6: InsertItem();
  • 4. break; case 7: exit(); default: puts("Invalid choice."); puts("nPress any key to go back to main menu."); getch(); break; } } } /* Function to Create the list*/ void CreateItem() { if(high==NULL) { printf("n --Creating the list--"); temp_node=NodeAlloc(); printf("n Enter starting data (as integer value) :"); scanf("%d",&temp_node->data); high=temp_node; } else{ printf("n List already created @ %d with %d as data.",high,high->data);}
  • 5. } /* Function to Append items to the list*/ void AppendItem() { low=high; if(high==NULL) { CreateItem(); } else { temp_node=NodeAlloc(); printf("n Enter Item (in integer) :"); scanf("%d",&temp_node->data); temp_node->rightlink=NULL; while(low->rightlink!=NULL) low=low->rightlink; low->rightlink=temp_node; temp_node->leftlink=low; last=low->rightlink; }
  • 6. } /* Function to Traverse the list both ways and print the data*/ void PrintItem() { DUBLL temp_node; if(high==NULL) { printf("n List is not available. Please create a list first."); getch(); CreateItem(); } temp_node=high; last=low->rightlink; printf("n--Printing The List In Forward direction--n"); while(temp_node!=NULL) //In forward direction { printf("t %d",temp_node->data); temp_node = temp_node->rightlink; } printf("n"); printf("n--Printing The List In Backward direction--n"); temp_node=high;
  • 7. if(temp_node->rightlink==NULL){printf("%d",temp_node->data);return; } while(last!=NULL) //In backward direction { printf("t %d",last->data); last = last->leftlink; } } /* Function to Delete items of the list*/ void DeleteItem() { int value; DUBLL temp_node; if(high==NULL) { printf("n List is not available. Please create a list first."); getch(); CreateItem(); } printf("n Item to delete :"); scanf("%d",&value); pntr=Search(value,1); pntr->leftlink->rightlink=pntr->rightlink; pntr->rightlink->leftlink=pntr->leftlink;
  • 8. temp_node=pntr; free(temp_node); } /* Function to Search an item from the list*/ DUBLL Search(int item,int flag) { temp_node = high; if(high==NULL) { printf("n List is not available. Please create a list first."); getch(); CreateItem(); } while(temp_node!=NULL) { if(temp_node->data==item ) { if(flag==0) { return(1); } else {
  • 9. return(temp_node); } } temp_node=temp_node->rightlink; } } /* Function to Allocate nodes*/ DUBLL NodeAlloc() { DUBLL tmep_node; tmep_node=malloc(sizeof(struct dubll)); if(tmep_node==NULL) { printf("n No memory available. Node allocation cannot be done."); } tmep_node->rightlink=tmep_node->leftlink=NULL; return(tmep_node); } /* Function to Insert items in the middle of the list*/ void InsertItem() { int node;
  • 10. DUBLL temp_node; if(high==NULL) { printf("n List is not available. Please create a list first."); getch(); CreateItem(); } temp_node=NodeAlloc(); printf("Position At which node to be inserted: ___ & New Item Value: ___ "); scanf("%d",&node); scanf("%d",&temp_node->data); pntr=Search(node,1); if(pntr->rightlink==NULL){printf("n The operation is not possible."); getch();return;} temp_node->leftlink=pntr; //creating link to new node temp_node->rightlink=pntr->rightlink; pntr->rightlink->leftlink=temp_node; pntr->rightlink=temp_node; printf("n Item has been Inserted."); getch(); }