Add to Favorites    Make Home Page 1294 Online  
 Language Categories  
 Our Services  

Home » C++ Home » Data Structures Home » Doubly linked lists

A D V E R T I S E M E N T

Search Projects & Source Codes:

Title Doubly linked lists
Author Gautam Arjun
Author Email gautam_arjun [at] hotmail.com
Description Doubly linked lists
Category C++ » Data Structures
Hits 371026
Code Select and Copy the Code
// Sorted Doubly Linked List with Insertion and Deletion #include <iostream> #include <cstdlib> #include <string> using namespace std; class Dllist { private: typedef struct Node { string name; Node* next; Node* prev; }; Node* head; Node* last; public: Dllist() { head = NULL; last = NULL; } bool empty() const { return head==NULL; } friend ostream& operator<<(ostream& ,const Dllist& ); void Insert(const string& ); void Remove(const string& ); }; void Dllist::Insert(const string& s) { // Insertion into an Empty List. if(empty()) { Node* temp = new Node; head = temp; last = temp; temp->prev = NULL; temp->next = NULL; temp->name = s; } else { Node* curr; curr = head; while( s>curr->name && curr->next != last->next) curr = curr->next; if(curr == head) { Node* temp = new Node; temp->name = s; temp->prev = curr; temp->next = NULL; head->next = temp; last = temp; // cout<<" Inserted "<<s<<" After "<<curr->name<<endl; } else { if(curr == last && s>last->name) { last->next = new Node; (last->next)->prev = last; last = last->next; last->next = NULL; last->name = s; // cout<<" Added "<<s<<" at the end "<<endl; } else { Node* temp = new Node; temp->name = s; temp->next = curr; (curr->prev)->next = temp; temp->prev = curr->prev; curr->prev = temp; // cout<<" Inserted "<<s<<" Before "<<curr->name<<endl; } } } } ostream& operator<<(ostream& ostr, const Dllist& dl ) { if(dl.empty()) ostr<<" The list is empty. "<<endl; else { Dllist::Node* curr; for(curr = dl.head; curr != dl.last->next; curr=curr->next) ostr<<curr->name<<" "; ostr<<endl; ostr<<endl; return ostr; } } void Dllist::Remove(const string& s) { bool found = false; if(empty()) { cout<<" This is an empty list! "<<endl; return; } else { Node* curr; for(curr = head; curr != last->next; curr = curr->next) { if(curr->name == s) { found = true; break; } } if(found == false) { cout<<" The list does not contain specified Node"<<endl; return; } else { // Curr points to the node to be removed. if (curr == head && found) { if(curr->next != NULL) { head = curr->next; delete curr; return; } else { delete curr; head = NULL; last = NULL; return; } } if (curr == last && found) { last = curr->prev; delete curr; return; } (curr->prev)->next = curr->next; (curr->next)->prev = curr->prev; delete curr; } } } int main() { Dllist d1; int ch; string temp; while(1) { cout<<endl; cout<<" Doubly Linked List Operations "<<endl; cout<<" ------------------------------"<<endl; cout<<" 1. Insertion "<<endl; cout<<" 2. Deletion "<<endl; cout<<" 3. Display "<<endl; cout<<" 4. Exit "<<endl; cout<<" Enter your choice : "; cin>>ch; switch(ch) { case 1: cout<<" Enter Name to be inserted : "; cin>>temp; d1.Insert(temp); break; case 2: cout<<" Enter Name to be deleted : "; cin>>temp; d1.Remove(temp); break; case 3: cout<<" The List contains : "; cout<<d1; break; case 4: system("pause"); return 0; break; } }

Related Source Codes

Script Name Author
Moving ball screen saver karlmarx
The Classic Game of Snake & Ladder Lakshmi Narayana .A
Railway seat reservation question which comes in sapient VyomWorld
To calculate percentile Ravi Mathur
Send to folder ANIMESH SAHU
Analog clock and calendar Nazia & Rida
HIGH/LOW GAME MOLLY ARORA
Data structure (stack Implimentation) Swapnil B Adsure
Memory Game AnirudhSanyal
Easy Calc Anirudh Sanyal
GK Quiz Anirudh Sanyal
Hangman Game Manish Jain
Snakeman Manish Jain
Full month Calendar Nigi
Cursor shapes nigi

A D V E R T I S E M E N T




Google Groups Subscribe to SourceCodesWorld - Techies Talk
Email:

Free eBook - Interview Questions: Get over 1,000 Interview Questions in an eBook for free when you join JobsAssist. Just click on the button below to join JobsAssist and you will immediately receive the Free eBook with thousands of Interview Questions in an ebook when you join.

New! Click here to Add your Code!


ASP Home | C Home | C++ Home | COBOL Home | Java Home | Pascal Home
Source Codes Home Page

 Advertisements  

Google Search

Google

Source Codes World.com is a part of Vyom Network.

Vyom Network : Web Hosting | Dedicated Server | Free SMS, GRE, GMAT, MBA | Online Exams | Freshers Jobs | Software Downloads | Interview Questions | Jobs, Discussions | Placement Papers | Free eBooks | Free eBooks | Free Business Info | Interview Questions | Free Tutorials | Arabic, French, German | IAS Preparation | Jokes, Songs, Fun | Free Classifieds | Free Recipes | Free Downloads | Bangalore Info | Tech Solutions | Project Outsourcing, Web Hosting | GATE Preparation | MBA Preparation | SAP Info | Software Testing | Google Logo Maker | Freshers Jobs

Sitemap | Privacy Policy | Terms and Conditions | Important Websites
Copyright ©2003-2024 SourceCodesWorld.com, All Rights Reserved.
Page URL: http://www.sourcecodesworld.com/source/show.asp?ScriptID=624


Download Yahoo Messenger | Placement Papers | Free SMS | C Interview Questions | C++ Interview Questions | Quick2Host Review