Date post: | 21-Jan-2016 |
Category: |
Documents |
Upload: | malcolm-doyle |
View: | 216 times |
Download: | 0 times |
CS212: DATASTRUCTURES
Lecture 3: Searching
1
2
SinglyLinked list
• 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
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++
//We use two classes: Node and List
#include<iostream>
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++
// 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++
7
Example 1 – C++
//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
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
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
List Class – insertNode methodpublic 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
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
List Class – insertFirst method
public void insertFirst(int dd){ Node newNode = new Node(dd); newNode.next = head; head = newNode; }
13
Example 1 – Java
Main Method Outputpackage 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(); }}