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.

Slides:



Advertisements
Similar presentations
Stacks, Queues, and Linked Lists
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Data Structures Linked Lists Linked List Basics. Array Disadvantages Arrays, although easy to understand have lots of disadvantages Contiguous Memory.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-5
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Chapter 17 Linked List Saurav Karmakar Spring 2007.
 A queue is a waiting line…….  It’s in daily life:-  A line of persons waiting to check out at a supermarket.  A line of persons waiting.
1 CSC 211 Data Structures Lecture 22 Dr. Iftikhar Azim Niaz 1.
Chapter Dr. Bernard Chen Ph.D. University of Central Arkansas Fall 2008.
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
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.
Programming Pointers. COMP104 Lecture 32 / Slide 2 Pointers l Pointers are objects whose values are the locations of other objects l Pointers are memory.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
UNIT-2. Singly Linked List Doubly Linked List Circular Linked List Representing Stack with Linked List. Representing Queue with Linked List.
Copyright © 2011 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Starting Out with C++ Early Objects Seventh Edition by Tony Gaddis, Judy.
1 Chapter 16-1 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion.
Comp 245 Data Structures Linked Lists. An Array Based List Usually is statically allocated; may not use memory efficiently Direct access to data; faster.
C++ Data Types Structured array struct union class Address pointer reference Simple IntegralFloating char short int long enum float double long double.
1 CS 132 Spring 2008 Chapter 3 Pointers and Array-Based Lists read p
A first look an ADTs Solving a problem involves processing data, and an important part of the solution is the careful organization of the data In order.
C++ Programming: From Problem Analysis to Program Design, Second Edition Chapter 17: Linked Lists.
1 Chapter 16 Linked Structures Dale/Weems. 2 Chapter 16 Topics l Meaning of a Linked List l Meaning of a Dynamic Linked List l Traversal, Insertion and.
1 Chapter 16 Linked Structures Dale/Weems/Headington.
Lists Chapter 8. 2 Linked Lists As an ADT, a list is –finite sequence (possibly empty) of elements Operations commonly include: ConstructionAllocate &
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
1. Circular Linked List In a circular linked list, the last node contains a pointer to the first node of the list. In a circular linked list,
Algorithms and Data Structures
LINKED LISTS Midwestern State University CMPS 1053 Dr. Ranette Halverson 1.
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
CIRCULAR L INKED L IST Insert as a first node Insert as a last node Delete first node Delete last node Insert after a node Insert before a node Search.
Circular Linked List Singly Circular Linked List Doubly Circular Linked List.
Circular linked list A circular linked list is a linear linked list accept that last element points to the first element.
CS162 - Topic #7 Lecture: Dynamic Data Structures –Review of pointers and the new operator –Introduction to Linked Lists –Begin walking thru examples of.
Data Structures AZHAR MAQSOOD NUST Institute of Information Technology (NIIT) Lecture 6: Linked Lists Linked List Basics.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Midterm 1 on Friday February 12 Closed book, closed notes No computer can be used 50 minutes 4 questions Write a function Write program fragment Explain.
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Data Structure & Algorithms
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Linked Lists Outline Introduction Self-Referential Structures.
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.
1 CMPT 117 Linked Lists (singly linked). 2 Problems with arrays  When an element is deleted from or inserted into an array, the rest of the array has.
 Review building a complete linked list  List traversal in main ( )  List traversal using a function.
  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.
UNIT-II Topics to be covered Singly linked list Circular linked list
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
Copyright © 2012 Pearson Education, Inc. Chapter 17: Linked Lists.
Prof. Amr Goneid, AUC1 CSCE 210 Data Structures and Algorithms Prof. Amr Goneid AUC Part R2. Elementary Data Structures.
1 Linked List. List vs Arrays Two built-in data structures that can be used to organize data, or to create other data structures: Lists Arrays.
UNIT – I Linked Lists.
Linked Lists Chapter 6 Section 6.4 – 6.6
UNIT-3 LINKED LIST.
Traversing a Linked List
Linked lists.
Chapter 4 Linked Lists
Sequences 9/18/ :20 PM C201: Linked List.
Chapter 16-2 Linked Structures
Chapter 4 Linked Lists.
Stack and Queues Stack implementation using Array
Queues FIFO Enqueue Dequeue Peek.
Linked Lists Chapter 4.
Chapter 4 Linked Lists.
Chapter 17: Linked Lists.
Chapter 16 Linked Structures
Linked Lists.
Stacks LIFO C C B B B B A A A A A Push (A) Push (B) Push (C) Pop Pop.
Linked lists.
Linked Lists.
Presentation transcript:

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 structures of variables are changed during the computation, they are called dynamic structures. There are next types of linked lists: Singly Linked List Circular Linked List Doubly-Linked List

struct LIST { int dann; LIST *next; }; LIST *head=NULL; LIST *rear=NULL; LIST *ptr=NULL; Singly Linked lists (in C++)

Operations List Building void make(int a) { ptr=new LIST; if (!head) head=ptr; else rear->next=ptr; ptr->dann=a; rear=ptr; rear->next=NULL; } // new ptr points to a LIST struct // If the list is empty, the ptr pointer changes the head pointer. // else change rear.next field from NULL to point // to the new node // store data in the new node. // set rear to point to the new node // set rear.next field equal to a null pointer - NULL

Operations List Building void make(int a) {ptr=new LIST; if (!head) head=ptr; else rear->next=ptr; ptr->dann=a; rear=ptr; rear->next=NULL; } next ptr head 1 rear NULL next ptr 2 NULL next 3 NULL

Traversing the list LIST* ptr = head; while (ptr != NULL) { // do something with *ptr node ptr = ptr->next; } Assign List head to node pointer While node pointer is not NULL Display the value member of the node pointed to by node pointer Assign node pointer to its own next member.

Traversing the list void print(void) {ptr=head; while (ptr){ cout dann << " "; ptr=ptr->next; } cout << endl; } 1next NULL head ptr

Destroying the list void clear(void) {while (head) { ptr=head->next; delete head; head=ptr; } 1next2 4 3 NULL head ptr head

Insertion a node void insert_after(int x, int a) { // Initialize ptr to head of list LIST *ptr=head; LIST *temp; // If the list is empty, do nothing if (!ptr)return; // Skip all nodes whose value is not equal to x while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) {// Allocate a new node temp and store data temp=new LIST; temp->dann=a; // Set temp.next field equal to a node is situated after ptr temp->next=ptr->next; // Change ptr.next field to point to the new node temp ptr->next=temp; }

Insertion a node void insert_after(int x, int a) {ptr=head; LIST *temp; if (!ptr) return; while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) { temp=new LIST; temp->dann=a; temp->next=ptr->next; ptr->next=temp; } 1next2 4 3 NULL head ptr next temp 99

Deleting a node void delete_after(int x) { // Initialize ptr to head of list LIST *ptr=head; LIST *temp; // If the list is empty, do nothing if (!ptr)return; // Skip all nodes whose value is not equal to x while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) {// Link the next field of ptr node to the node after temp temp=ptr->next; ptr->next=temp->next; // Delete temp delete temp; }

Deleting a node void delete_after(int x) { LIST *ptr=head; LIST *temp; if (!ptr)return; while (ptr != NULL && ptr->dann!=x) ptr=ptr->next; if (ptr!=NULL) { temp=ptr->next; ptr->next=temp->next; delete temp; } 1next2 4 3 NULL head ptr temp