Beruflich Dokumente
Kultur Dokumente
PROGRAM CODE:
//Class to create a node for a doubly linked list class Node { int num; Node pre, next; //Non-parameterized Constructor Node() { num=0; pre=next=null; }//Endconstructor //parameterized constructor Node(int n, Node pr, Node ne) { num=n; pre=pr; next=ne; }//End constructor }//End class //to create Doubly Linkedlist and perform other functions on it class DoublyList { Node head;//external reference variable to point to the beginning of list public DoublyList()//constructor to intialize head { head=null; }//End constructor //to add a node in the beginning of list public void addBeg(int n) {
Node ob=new Node(); ob.num=n; ob.pre=null; if(head==null)//if first node is created { ob.next=null; } else//if not the first node { ob.next=head; head.pre=ob; } head=ob;//make head always point to the beginning of list }//End method //to append a node public void addEnd(int n) { Node ob=new Node(); ob.num=n; ob.next=null; if(head==null)//if first node is created make head point to it { ob.pre=null; head=ob; } else { Node temp=head;//secure head and traverse by temp while(temp.next!=null)//to reach to the last node { temp=temp.next; } temp.next=ob;//connect the last node to the new node ob.pre=temp; } }//Endmethod //to display the elements of list public void display() { Node temp=head; while(temp!=null) { System.out.print(temp.num+ ); temp=temp.next; } System.out.println(\n);
}//Endmethod //to delete the nodefrom list where n matches num public void delete(int n) { Node temp=head; while(temp!=null) { if(temp.num==n) { if(temp==head) { temp.next.pre=null; head=temp.next; } else if(temp.next==null) { temp.pre.next=null; } else { temp.pre.next=temp.next; temp.next.pre=temp.pre; } break; } else temp=temp.next; } if(temp==null) System.out.println(\n+n+ not present in the list); }//Endmethod //to count and return the number of nodes in the list public int count() { Node temp=head; int c=0; while(temp!=null) { c++; temp=temp.next; } return c; }//Endmethod //to insert a node at position p in the list //Assuming it will not be the first and last node public void insertAt(int p, int n)
{ int c=count(); if(c<p) System.out.println(Position + p + does not exists in the list); else { Node temp=head; for(int i=1; i { temp=temp.next; } Node ob=new Node(); ob.num=n; ob.pre= temp; ob.next=temp.next; temp.next=ob; ob.next.pre=ob; }//else ends }//method ends } //class ends //To test the class DoublyList public class TestDoublyList { public void main() { DoublyList ob=new DoublyList(); //adding nodes in beginning ob.addBeg(1); ob.addBeg(2); ob.addBeg(3); System.out.println(****List elements after adding 3 numbers in beginning****); ob.display(); //adding 3 numbers at the end of ist ob.addEnd(4); ob.addEnd(5); ob.addEnd(6); System.out.println(****List elements after adding 3 numbers in beginning****); ob.display(); //deleting an element from list ob.delete(4); System.out.println(****List elements after deleting 4 from the list****); ob.display(); //counting number of elements in list int c=ob.count(); System.out.println(****** Number of elements in list are*******\n+c); //inserting an element in the list at specified position
System.out.println(\n****List elements after adding 4 at position 8 ****); ob.display(); ob.insertAt(8,4); }//method ends }//class ends
EXPLANATION:
The above program makes use of three classes: 1. Class Node: to create a node for a doubly linked list. It has two self referential pointers pre and next to point to the previous and next nodes respectively. It has an int variable num to store an integer. 2. Class Doublylist- It has various methods to test the different functionality of a doubly linked list. 3. Class TestDoublyLinkedList - to test the class DoublyLinkedList. Makes an object of this class and calls its various methods. For singly Linked List, click here: Link For Computational Complexity, click here: Link April 25, 2011 at 8:39 pm Leave a comment
Authors
Amita Suri
Categories
Ask for Online Tuitions (1) C++ (1) o Questions and Answers (1) Computer Organisation (1) o Questions and Answers (1) Data Structures (9) o Computational Complexity (2) Questions and Answers (1) o Linked Lists (4) o Queues (1) o Stacks (2)
ICSE_Computer Applications (13) o Detailed Syllabus (1) o Previous Yr Papers (7) o Sample Papers (3) o Specimen Paper (2) ISC_Computer Science (10) o Previous Yrs Practical Papers (7) o Sample Papers(Practical) (2) o Sample Papers(Theory) (1) Java (31) o Abstraction (1) o Arrays (3) Array Programs (2) o Building Applications(Classes and Objects) (4) o Inheritance (1) o Interface (1) o Number Programs (10) o Programs using If Loops Counters Accumulators (2) o Searching and Sorting Programs (6) o Series Based Programs (1) o Strings (2) String Programs (1) o Understanding Patterns (1) Various Pattern Programs (1)
All Programs
All_Prime_factors Animal_Dog_DogUser Class Armstrong Array Random Numbers Array_Of_Objects Binary to Decimal Bubble Sort Car_CarLot_LotUserFrank Class Class Car Class Polygon Comparing two lists program Consecutive Numbers Forming the Number itself Decimal To Binary Distance between places Doubly Linked List Program Egg Hunt Fibonacci series Figure_Rectangle-Triangle_Class_Program HCF Insertion Sort Kaprekar Numbers Keith Numbers LCM Linear and Binary Search Loops with sentinel values MergeSort Miles per Gallons Palindrome Perfect Number Pythagorean triplets Queue Using Arrays QuickSort Quiz Program Recursive SinglyLinkedList Roots_of_Quadratic_Equation Saddle Number Selection Sort Singly Linked List Smith Number Special Number Stack using Array StacK_Using_Interface_Program Sum_Of_Fractions Tips for ICSE Exam Tribonacci series
Email Subscription
An email was just sent to confirm that you want to follow this blog. Please find the email now and click activate. Enter your email address to subscribe to this blog and receive notifications of new posts by email. Join 24 other followers
Th