Menu DaniWeb
Log In Sign Up
  • Read
  • Contribute
  • Meet
  1. Home
  2. Programming Forum
  3. Software Development Forum
  4. Code Snippet Repository
  5. Reusable Code Snippet

Operations in doubly link list using header->next

14 Years Ago ranit 0 Tallied Votes 661 Views Share

hi
this is a program to perform insertions and deletion in doubly link list...

c++
#include<iostream.h>
#include<conio.h>
class dll
{
 struct dll_node
 {
 dll_node *prev,*next;
 int data;
 }*header,*first,*last,*nn;
 public:
 dll()
 {
 header=new dll_node;
 header->data=0;
 header->next=NULL;
 header->prev=NULL;
 last=NULL;
 }

 ~dll()
 void create(int);
 void display();
 void insertatbeg(int);
 void insertatend(int);
 dll_node* search(int);
 void insertkey(int,int);
 void insertbeforekey(int,int);
 void deleteatbeg();
};

void dll::create(int k)
{
dll_node *nn;
for(int i=1;i<=k;i++)
{
 nn=new dll_node;
 nn->data=i*10;
 nn->prev=NULL;
 nn->next=NULL;
 if(header->next==NULL)
 header->next=last=nn;
 else
 {
 nn->prev=last->next;
 last->next=nn;
 last=nn;
 }
}
display();
}
void dll::display()
{
dll_node *ptr;
cout<<endl;
for(ptr=header->next;ptr!=NULL;ptr=ptr->next)
cout<<ptr->data<<"->";
}
void dll::insertatbeg(int d)
{
nn=new dll_node;
nn->data=d;
 if(header->next==NULL)
 {
  nn->next=NULL;
  nn->prev=header;
  header->next=nn;
  last=nn;
  }
  else
  {

  nn->prev=header;
  nn->next=header->next;
  header->next=nn;
  nn->next->prev=nn;
  }
display();
}
void dll::insertatend(int d)
{
nn=new dll_node;
nn->data=d;
if(last==NULL)
{
header->next=last=nn;
}
else
{
nn->next=NULL;
nn->prev=last->next;
last->next=nn;
last=nn;
}
display();
cout<<"last="<<last->data;
}
dll_node* dll::search(int key)
{
dll_node *ptr;
for(ptr=header->next;ptr->next!=NULL && ptr->data!=key;ptr=ptr->next);
if(ptr!=NULL)
cout<<"Key "<<ptr->data<<" found";
else
cout<<"key "<<key<<" not found";
return ptr;
}

void dll::insertbeforekey(int key,int d)
{
dll_node *ptr,*nn;
nn=new dll_node;
nn->data=d;
ptr=search(key);
if(ptr!=NULL)
{
nn->next=ptr;
nn->prev=ptr->prev;
ptr->prev=nn;
nn->prev->next=nn;
}
else
cout<<"Insertion failed";
display();
}
void dll::insertafterkey(int key,int d)
{
dll_node *ptr;
nn=new dll_node;
nn->data=d;
ptr=search(key);
if(ptr!=NULL)
{

nn->next=ptr->next;
nn->prev=ptr;
ptr->next=nn;
nn->next->prev=nn;
}
else
cout<<"Insertion failed";
cout<<"last="<<last->data;
display();
}
void dll::deleteatbeg()
{
dll_node *ptr;
ptr=header->next;
if(header->next==NULL)
cout<<"Deletion is not possible";
else
header->next=ptr->next;
delete ptr;
cout<<"last="<<last->data;
display();
}
int main()
{
clrscr();
dll d;
cout<<"Create:";
d.create(7);
cout<<"\n\nInsert at beg:";
d.insertatbeg(58);
cout<<"\n\nInsert at end:";
d.insertatend(74);
cout<<"\n\nInsert before key:";
d.insertbeforekey(40,9);
cout<<"\n\nInsert after key:";
d.insertafterkey(40,5);
cout<<"\n\ndelete at beg:";
d.deleteatbeg();
getch();
return 0;
}

}
About the Author
Member Avatar for ranit
ranit 0 Newbie Poster
Member Avatar for griswolf
griswolf 304 Veteran Poster
14 Years Ago

This is C++ code, and should go in the C++ forum.

To make it easier to understand, please wrap your code in [code] tag. You can do that by selecting your code in the editor window and pressing the '#' button. Or see what that does, and duplicate it in your own editor before you paste it.

Reply to this topic
Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.

Sign Up — It's Free!
Related Topics
  • Member Avatar Doubly Linked List Help 16
  • Member Avatar Doubly linked list compile error 4
  • Member Avatar ToolStripMenuItem - Website 4
  • Member Avatar Doubly Linked List Implementation 2
  • Member Avatar datetimepicker problem in C# 9
  • Member Avatar Doubly Linked List Remove All Nodes 3
  • Member Avatar simple python web server 2
  • Member Avatar Using graphics in C++ game coding 5
  • Member Avatar filecopy 3
  • Member Avatar using crypt in c 5
  • Member Avatar Guessing Game 3
  • Member Avatar Using filter in c# 3
  • Member Avatar File problem in link list 5
  • Member Avatar Using libcurl in C++ with Linux 2
  • Member Avatar Simple download helper - need help! 9
  • Member Avatar reading mpeg4 header in c# 4
  • Member Avatar Beginner needs help with Pygame 9
  • Member Avatar inserting a node in middle of doubly linked list 10
  • Member Avatar Insertion Into a Doubly Linked List 1
  • Member Avatar Deleting nodes in a doubly linked list 6
Not what you need?

Reach out to all the awesome people in our software development community by starting your own topic. We equally welcome both specific questions as well as open-ended discussions.

Start New Topic
Topics Feed
Reply to this Topic
Edit Preview

Share Post

Insert Code Block

  • Forums
  • Forum Index
  • Hardware/Software
    • Recommended Topics
  • Programming
    • Recommended Topics
  • Digital Media
    • Recommended Topics
  • Community Center
    • Recommended Topics
  • Latest Content
  • Newest Topics
  • Latest Topics
  • Latest Posts
  • Latest Comments
  • Top Tags
  • Topics Feed
  • Social
  • Top Members
  • Meet People
  • Community Functions
  • DaniWeb Premium
  • Newsletter Archive
  • Markdown Syntax
  • Community Rules
  • Developer APIs
  • Connect API
  • Forum API Docs
  • Tools
  • SEO Backlink Checker
  • Legal
  • Terms of Service
  • Privacy Policy
  • FAQ
  • About Us
  • Advertise
  • Contact Us
© 2025 DaniWeb® LLC