Linked Lists 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.

Slides:



Advertisements
Similar presentations
Linked Lists Geletaw S..
Advertisements

Pointers and Data Structures 1 Yinzhi Cao Modified from slides made by Prof Goce Trajcevski.
Chapter 17 Linked Lists.
COMP171 Fall 2005 Lists.
Linked Lists.
DATA STRUCTURES USING C++ Chapter 5
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Section 2.5 Single-Linked Lists. A linked list is useful for inserting and removing at arbitrary locations The ArrayList is limited because its add and.
DATA STRUCTURE “Linked Lists” SHINTA P STMIK MDP April 2011.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
Circular Linked List. Agenda  What is a Circularly linked list  Why a Circular Linked List  Adding node in a new list  Adding node to list with nodes.
Linked List
Chapter 13 Pointers and Linked Lists. Nodes and Linked Lists Linked list: A sequence of nodes in which each node is linked or connected to the node preceding.
The Template Class Chain Chain Linear list. Each element is stored in a node. Nodes are linked together using pointers.
Stacks Using Linked Lists Here, we use the same concept of the stack but eliminate the MAXIMUM data items constraint. Since we shall be using Linked Lists.
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 17 Linked.
Linked Lists Spring Linked Lists / Slide 2 List Overview * Linked lists n Abstract data type (ADT) * Basic operations of linked lists n Insert,
Reference: Vinu V Das, Principles of Data Structures using C and C++
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Linked Lists part 2 CS 244 Brent M. Dingle, Ph.D. Game Design and Development Program Department of Mathematics, Statistics, and Computer Science University.
1 Data Structures CSCI 132, Spring 2014 Lecture 20 Linked Lists.
Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy Walters, and Godfrey Muganda Chapter 17: Linked Lists.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Pointer Variables int i; declares an int variable and sets aside a named memory location to store the int int * iptr; declares a variable that holds the.
Review 1 Polish Notation Prefix Infix Postfix Precedence of Operators Converting Infix to Postfix Evaluating Postfix.
Trees 2015, Fall Pusan National University Ki-Joune Li.
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.
1 CSE 2341 Object Oriented Programming with C++ Note Set #21.
CHAPTER 17 LINKED LISTS. In this chapter, you will:  Learn about linked lists  Become aware of the basic properties of linked lists  Explore the insertion.
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
LINEAR LINKED LISTS The disadvantages of arrays: 1.The size of the array is fixed. 2.Large size of array??? 3. Inserting and deleting elements. If the.
Linked List.  Is a series of connected nodes, where each node is a data structure with data and pointer(s) Advantages over array implementation  Can.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
1 Linked List. 2 List A list refers to a sequence of data items  Example: An array The array index is used for accessing and manipulation of array elements.
1 Linked Multiple Queues. 2 A real world example. Not in the book. Sometimes we have a fixed number of items that move around among a fixed set of queues.
1 CS 132 Spring 2008 Chapter 5 Linked Lists p
Doubly Linked List Exercises Sometimes it is useful to have a linked list with pointers to both the next and previous nodes. This is called a doubly linked.
Linked Lists Chapter Introduction To The Linked List ADT Linked list: set of data structures (nodes) that contain references to other data structures.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
Copyright © 2014, 2008 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Eighth Edition by Tony Gaddis,
Binary Search Trees. 2 Overview Recursive linked list ops Binary Trees.
  A linked list is a collection of components called nodes  Every node (except the last one) contains the address of the next node  The address of.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1 CSE 2341 Object Oriented Programming with C++ Note Set #18.
More on Linked Lists, Stacks, and Queues
UNIT – I Linked Lists.
Linked List.
Lists CS 3358.
UNIT-3 LINKED LIST.
Linked lists.
Linked lists Motivation: we can make arrays, but their functionality is slightly limited and can be difficult to work with Biggest issue: size management.
Sequences 9/18/ :20 PM C201: Linked List.
Lecture 4 Linked List and STL
Recursion.
Chapter 16-2 Linked Structures
Chapter 18: Linked Lists.
Cs212: Data Structures Computer Science Department Lab 7: Stacks.
[Chapter 4; Chapter 6, pp ] CSC 143 Linked Lists [Chapter 4; Chapter 6, pp ]
Chapter 17: Linked Lists Starting Out with C++ Early Objects
Chapter 17: Linked Lists.
Linked Lists.
CS148 Introduction to Programming II
Linked List Improvements
Linked lists.
Linked Lists Dr. Jose Annunziato.
Linked Lists.
Presentation transcript:

Linked Lists 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 A P BC

Node A datapointer node

#include class linklist { private: struct node { int data; node *link; }*p; public: linklist(); void append( int num ); void add_as_first( int num ); void addafter( int c, int num ); void del( int num ); void display(); int count(); ~linklist(); }; A Simple Linked List Class

Constructor linklist::linklist() { p=NULL; }

Inserting a new node Possible cases: 1.Insert into an empty list 2.Insert as a first node in the list 3.Insert at back of the list (append) 4.Insert in middle of the list

Append Function void linklist::append(int num) { node *q,*t; if( p == NULL ) { p = new node; p->data = num; p->link = NULL; } else { q = p; while( q->link != NULL ) q = q->link; t = new node; t->data = num; t->link = NULL; q->link = t; }

Add_as_ first Function void linklist::add_as_first(int num) { node *q; q = new node; q->data = num; q->link = p; p = q; } newNode P 1 2

Addafter Function void linklist::addafter( int c, int num) { node *q,*t; int i; for(i=0,q=p;i<c;i++) { q = q->link; if( q == NULL ) { cout<<"\nThere are less than "<<c<<" elements."; return; } t = new node; t->data = num; t->link = q->link; q->link = t; } newNode 1 2

Del Function void linklist::del( int num ) { node *q,*r; q = p; if( q->data == num ) { p = q->link; delete q; return; } r = q; while( q!=NULL ) { if( q->data == num ) { r->link = q->link; delete q; return; } r = q; q = q->link; } cout<<"\nElement "<<num<<" not Found."; }

Display Function void linklist::display() { node *q; cout<<endl; for( q = p; q != NULL; q = q->link ) cout data; }

Count Function int linklist::count() { node *q; int c=0; for( q=p; q != NULL; q = q->link ) c++; return c; }

Destructor Function linklist::~linklist() { node *q; if( p == NULL ) return; while( p != NULL ) { q = p->link; delete p; p = q; }