Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece.

Similar presentations


Presentation on theme: "CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece."— Presentation transcript:

1 CS212: DATASTRUCTURES Lecture 3: Searching 1

2 SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece of data (any type) – Pointer to the next node in the list Head: pointer to the first node. The last node points to NULL.

3 Example 1 3  Write program that create an Integer linked list: {50,60,70,80,90}.  Add a member function to the class Int_list which:  Add node at a list.

4 Example 1 – C++ 4 //We use two classes: Node and List #include using namespace std; // we start with class node for nodes. class node{ public: node(int y){ info=y; next=0;} int info; node *next;};

5 Example 1 – C++ 5 // Declare class list class list{ private: node *head, *tail; int count; public: list(){head=tail=0; count=0;} // Declare addnode function in next slide };

6 Example 1 – C++ 6

7 7 //Using List : void main() {list s; s.addNode(10); s.addNode(12); s.addNode(11); s.addNode(13); s.addNode(5); s.addNode(10); }

8 Example 1 – Java 8  Node Class class Node { public int iData; public Node next; public Node(int dd){ iData=dd; } public void displayLink(){ System.out.print("{"+iData+"} "); }

9 Example 1 – Java 9  List Class class List{ private Node head; private Node last; public int count; public List(){ // next by default initialized to NULL count=0; head = null; last = null; }

10 Example 1 – Java 10  List Class – insertNode method public void insertNode(int key){ // insert in order Node newNode = new Node(key); // make new node Node pre = null; Node current = head; // start at first node "head" // until end of list, while(current != null && key > current.iData) { pre = current; current = current.next; // go to next item } if (current == null || key < current.iData){ if(pre==null){ // at beginning of list head = newNode;// head --> newNode newNode.next = current; }// newNode --> Ploc else{ // not at beginning, maybe in a middle or ending of the list pre.next = newNode; // old Ppre --> newNode newNode.next = current; }// newNode --> Ploc count++; // increament the counter } else // if a key is already exist in a list System.out.println("Duplicated data"); } // end insertNode()

11 Example 1 – Java 11  List Class – displayList method public void displayList(){ System.out.print("List (first-->last): "); Node current = head; while (current != null){ current.displayNode(); current = current.next; } System.out.println(""); }

12 Example 1 – Java 12  List Class – insertFirst method public void insertFirst(int dd){ Node newNode = new Node(dd); newNode.next = head; head = newNode; }

13 Example 1 – Java 13  Main Method  Output package javaapplication12; public class JavaApplication12 { public static void main(String[] args) { List s = new List(); s.insertNode(50); s.insertNode(60); s.insertNode(70); s.insertNode(80); s.insertNode(90); s.displayList(); s.insertNode(66); s.displayList(); s.insertFirst(88); s.displayList(); }


Download ppt "CS212: DATASTRUCTURES Lecture 3: Searching 1. SinglyLinked list 2 A linked list is a series of connected nodes. Each node contains at least: – A piece."

Similar presentations


Ads by Google