• Share
  • Email
  • Embed
  • Like
  • Save
  • Private Content
Computer notes  - Linked List
 

Computer notes - Linked List

on

  • 1,442 views

Create a new node in memory to hold ‘9’. Link the new node into the list. C++ Code for Linked List

Create a new node in memory to hold ‘9’. Link the new node into the list. C++ Code for Linked List

Statistics

Views

Total Views
1,442
Views on SlideShare
1,441
Embed Views
1

Actions

Likes
1
Downloads
90
Comments
0

1 Embed 1

http://192.168.33.10 1

Accessibility

Categories

Upload Details

Uploaded via as Microsoft PowerPoint

Usage Rights

© All Rights Reserved

Report content

Flagged as inappropriate Flag as inappropriate
Flag as inappropriate

Select your reason for flagging this presentation as inappropriate.

Cancel
  • Full Name Full Name Comment goes here.
    Are you sure you want to
    Your message goes here
    Processing…
Post Comment
Edit your comment
  • End of lecture 2 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • The first goal is a worldview to adopt The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.
  • End of lecture 3 The second goal is the “nuts and bolts” of the course. The third goal prepares a student for the future.

Computer notes  - Linked List Computer notes - Linked List Presentation Transcript

  • Class No.03 Data Structures http://ecomputernotes.com
  • Linked List
    • Actual picture in memory:
    1051 1052 1055 1059 1060 1061 1062 1063 1064 1056 1057 1058 1053 1054 2 6 8 7 1 1051 1063 1057 1060 0 head 1054 1063 current 6 8 7 1 head current 1065 http://ecomputernotes.com 2
  • Linked List Operations
    • add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9);
    http://ecomputernotes.com 9 newNode
  • Linked List Operations
    • add(9): Create a new node in memory to hold ‘9’ Node* newNode = new Node(9);
    • Link the new node into the list
    http://ecomputernotes.com 9 newNode 2 6 8 7 1 head current size=5 6 9 newNode 1 3 2
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • The Node class
    • class Node {
    • public:
    • int get() { return object; };
    • void set(int object) { this->object = object; };
    • Node *getNext() { return nextNode; };
    • void setNext(Node *nextNode)
    • { this->nextNode = nextNode; };
    • private:
    • int object;
    • Node *nextNode;
    • };
    http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
    • #include <stdlib.h>
    • #include &quot;Node.cpp&quot;
    • class List {
    • public:
    • // Constructor
    • List() {
    • headNode = new Node();
    • headNode->setNext(NULL);
    • currentNode = NULL;
    • size = 0;
    • };
    C++ Code for Linked List http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • C++ Code for Linked List
    • void add(int addObject) {
    • Node* newNode = new Node();
    • newNode->set(addObject);
    • if( currentNode != NULL ){
    • newNode->setNext(currentNode->getNext());
    • currentNode->setNext( newNode );
    • lastCurrentNode = currentNode;
    • currentNode = newNode;
    • }
    • else {
    • newNode->setNext(NULL);
    • headNode->setNext(newNode);
    • lastCurrentNode = headNode;
    • currentNode = newNode;
    • }
    • size++;
    • };
    http://ecomputernotes.com 
  • Building a Linked List headNode size=0 List list; http://ecomputernotes.com
  • Building a Linked List headNode 2 headNode currentNode size=1 lastcurrentNode size=0 List list; list.add(2); http://ecomputernotes.com
  • Building a Linked List headNode 2 headNode currentNode size=1 lastcurrentNode 2 6 headNode currentNode size=2 lastcurrentNode size=0 List list; list.add(2); list.add(6); http://ecomputernotes.com
  • Building a Linked List
    • List.add(8); list.add(7); list.add(1);
    2 6 1 headNode currentNode size=5 lastcurrentNode 8 http://ecomputernotes.com 7
  • C++ Code for Linked List
    • int get() {
    • if (currentNode != NULL)
    • return currentNode->get();
    • };
    http://ecomputernotes.com
  • C++ Code for Linked List
    • bool next() {
    • if (currentNode == NULL) return false;
    • lastCurrentNode = currentNode;
    • currentNode = currentNode->getNext();
    • if (currentNode == NULL || size == 0)
    • return false;
    • else
    • return true;
    • };
    http://ecomputernotes.com