cs212: datastructures lecture 3: searching 1. singlylinked list 2 a linked list is a series of...
TRANSCRIPT
![Page 1: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/1.jpg)
CS212: DATASTRUCTURES
Lecture 3: Searching
1
![Page 2: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/2.jpg)
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.
![Page 3: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/3.jpg)
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.
![Page 4: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/4.jpg)
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;};
![Page 5: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/5.jpg)
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
};
![Page 6: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/6.jpg)
6
Example 1 – C++
![Page 7: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/7.jpg)
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);
}
![Page 8: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/8.jpg)
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+"} "); }
}
![Page 9: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/9.jpg)
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;
}
}
![Page 10: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/10.jpg)
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()
![Page 11: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/11.jpg)
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(""); }
![Page 12: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/12.jpg)
12
Example 1 – Java
List Class – insertFirst method
public void insertFirst(int dd){ Node newNode = new Node(dd); newNode.next = head; head = newNode; }
![Page 13: 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](https://reader036.vdocuments.net/reader036/viewer/2022062409/5697c0141a28abf838ccd5ff/html5/thumbnails/13.jpg)
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(); }}