D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT. P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to.

Slides:



Advertisements
Similar presentations
Linked Lists.
Advertisements

DATA STRUCTURES USING C++ Chapter 5
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
CHP-5 LinkedList.
Singly Linked List BTECH, EE KAZIRANGA UNIVERSITY.
Data Structure Lecture-3 Prepared by: Shipra Shukla Assistant Professor Kaziranga University.
Introduction to Linked Lists In your previous programming course, you saw how data is organized and processed sequentially using an array. You probably.
Data Structures: A Pseudocode Approach with C
Linked Lists Compiled by Dr. Mohammad Alhawarat CHAPTER 04.
Nirmalya Roy School of Electrical Engineering and Computer Science Washington State University Cpt S 122 – Data Structures Data Structures.
1 Pointers A pointer variable holds an address We may add or subtract an integer to get a different address. Adding an integer k to a pointer p with base.
COMP103 - Linked Lists (Part A)1 Chapter 17 Truly dynamic memory management.
1 ES 314 Advanced Programming Lec 3 Sept 8 Goals: complete discussion of pointers discuss 1-d array examples Selection sorting Insertion sorting 2-d arrays.
Chapter 4 Linked Lists. © 2005 Pearson Addison-Wesley. All rights reserved4-2 Preliminaries Options for implementing an ADT List –Array has a fixed size.
1 Procedural Concept The main program coordinates calls to procedures and hands over appropriate data as parameters.
CHAPTER 3 : STACKS 3.1 Understand Stacks 3.2 Implement the operation of stack By : Suzila Yusof.
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 10. Pointers & Dynamic Data Structures.
Reference: Vinu V Das, Principles of Data Structures using C and C++
2 Preliminaries Options for implementing an ADT List Array has a fixed size Data must be shifted during insertions and deletions Linked list is able to.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley. Ver Chapter 4: Linked Lists Data Abstraction & Problem Solving with.
Implementation of Linked List For more notes and topics visit: eITnotes.com.
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.
Lists 1. Introduction Data: A finite sequence of data items. Operations: Construction: Create an empty list Empty: Check if list is empty Insert: Add.
Pointers OVERVIEW.
Chapter 1 Object Oriented Programming. OOP revolves around the concept of an objects. Objects are created using the class definition. Programming techniques.
Lists II. List ADT When using an array-based implementation of the List ADT we encounter two problems; 1. Overflow 2. Wasted Space These limitations are.
CPSC 252 Dynamic Memory Allocation Page 1 Dynamic memory allocation Our first IntVector class has some serious limitations the capacity is fixed at MAX_SIZE.
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 &
Data Structures. Abstract Data Type A collection of related data is known as an abstract data type (ADT) Data Structure = ADT + Collection of functions.
Chapter Lists Dr. Bernard Chen Ph.D. University of Central Arkansas Spring 2010.
Pointer. lvalues In C++, any expression that refers to an internal memory location is called an lvalue Appear on left side of assignment statement e.g.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI CSCI 240 Elementary Data Structures Linked Lists Linked Lists Dale.
 Array is a data structure were elements are stored in consecutive memory location.in the array once the memory is allocated.it cannot be extend any more.
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.
 Memory from the heap  Dynamic memory allocation using the new operator  Build a dynamic linked list  Another way of traversing a linked list 
1 Linked List. Outline Introduction Insertion Description Deletion Description Basic Node Implementation Conclusion.
Linked List. LINKED LIST Link is collection of similar type of elements. There are two ways of maintaining a list in memory. The first way is store the.
Data Structure & Algorithms
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.
  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
Data Structure and Algorithm: CIT231 Lecture 6: Linked Lists DeSiaMorewww.desiamore.com/ifm1.
LINKED LISTS.
© Oxford University Press All rights reserved. Data Structures Using C, 2e Reema Thareja.
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.
STACKS & QUEUES for CLASS XII ( C++).
Linked List :: Basic Concepts
Lectures linked lists Chapter 6 of textbook
Review Deleting an Element from a Linked List Deletion involves:
Chapter 4 Linked Lists.
Linked list.
Linked lists.
Linked Lists head One downside of arrays is that they have a fixed size. To solve this problem of fixed size, we’ll relax the constraint that the.
CSCE 210 Data Structures and Algorithms
Chapter 4 Linked Lists
LINKED LISTS CSCD Linked Lists.
Review & Lab assignments
Linked Lists Chapter 4.
Linked List.
Chapter 16 Linked Structures
Introduction to C++ Linear Linked Lists
Linked lists.
Linked Lists.
Presentation transcript:

D ATA S TRUCTURE Ali Abdul Karem Habib MSc.IT

P OINTER A pointer is a variable which represents the location of a data item. We can have a pointer to any variable type. Pointer Operators - The address operator & gives the ``address of a variable''. - The indirection operator * gives the ``contents of an object pointed to by pointer''. To declare a pointer to a variable do: int *ptr;

P OINTER I N C++ Declare pointers to any data type using dereference operator * int * int_ptr; char * ch_ptr; A pointer takes its value as the address in memory of another variable int b, a = 6; int* a_ptr; a_ptr = &a; /* ‘&’ : address of operator */ B= *a_ptr ; /* ‘*’ : dereference operator */ 6 address of ‘a’ a a_ptr

P OINTER E XAMPLE Consider the effect of the following code: int x = 1, y = 2; int *ip; /*pointer to an integer*/ ip = &x; /*assign address of x to ip*/ y = *ip; /*assign value of x to y*/ x = ip; /*assigns address of x to x*/ *ip = 3; /*assigns 3 to value of x*/

E XAMPLE O F POINTER I N C++ # include Void main () { Int a =30, *ptr ; ptr=&a; Cout<<“ Print the address Of a “<<endl; Cout<< ptr; Cout<< “ The containt Of a “<< endl; Cout<<*ptr; getch(); }

The new operator p = new int Dynamic allocation of a memory cell that can contain an integer. The delete operator returns dynamically allocated memory to the system for reuse. A pointer to a deallocated memory (*p) cell is p = NULL

( a) Declaring pointer variables (b) pointing to statically allocated memory (c) assigning a value ( d) allocating memory dynamically (e) assigning a value

(f) copying a pointer (g) allocating memory dynamically and assigning a value (h) assigning NULL to a pointer variable (i) deallocating memory

L INKED L IST It is another type of data structure which are dynamic allocation. It is collection of especially designed data elements called nodes linked to one another by means of pointers. Each node is divided into 2 parts first part contains the Data and the second contains Pointer which points to the next node.

A DVANTAGES OF L INKED L IST OVER A RRAY Array have a fixed dimension. Once the size of an array is decided it can not be increased during execution. Array elements are always stored in contiguous memory locations. Insertion and deletion in array is complex because it requires shifting of elements in array. It is a dynamic data structure, i.e. a linked list can grow and shrink in size during its lifetime. The nodes of linked list (elements) are stored at different memory locations. Insertion and deletion in linked list is easy because it does not requires shifting of elements in it.

O PERATION O N L INKED L IST Creation :- Creation operation is used to create a linked list which one node. Insertion : - Insertion operation is used to insert a new node at any specified location in the linked list. A new node may be inserted. (a) At the beginning of the linked list (b) At the end of the linked list (c) At any specified position in between in a linked list Deletion :- Same Above

O PERATION O N L INKED L IST … C ONT Traversing : - Traversing is the process of going through all the nodes from one end to another end of a linked list. Searching :- Usually searching operations is employed not only while a data item is needed but in case of insertion and deletion at specified location search operation is performed before nodes can be inserted or deleted.

T YPES O F L INKED L IST Basically we can divide the linked list into the following three types in the order in which node are arranged. Singly linked list Doubly linked list Circular linked list

A S INGLY L INKED L IST is one in which all nodes are linked together in some sequential manner. By single pointer Hence it is also called Linear linked list. Each node in a singly Linked List has two parts one to hold the data and other to point to address of the successor node (next node). The problem with this list is that we cannot access the predecessor nodes from the current i.e. we cannot backward traverse in a singly linked list.

C REATE N ODE U SING C++ A node in a linked list is usually a struct struct Node { int data ; Node *next; }; Node * Start, *Cur; Node *p; // pointer to node p = new Node; // allocate node Note :- Access to the Node elements by Pointer using -> operator p->data, p->next Access to the Node Structure by reference of structure using Node item ; item. data, item. next

A LGORITHM TO C REATE FIRST N ODE 1- Start 2- If Start=Null then 3- Prepare Node 4- Read data 5- Node ->data= data 6- Node ->next=Null 7- Start = Node 8- Cur = Node 9- End. DataNull Start Cur Node

I NSERT A T F IRST 1- Start 2- Prepare Node 3- Read Data 4- Node ->Data=Data 5- Node->next=Start 6- Start=Node 7-End. DataNull Start Cur Data New Node

I NSERT A T L AST 1- Start 2- Prepare Node 3- Read Data 4- Node ->Data=Data 5- Cur ->next=Node 6- Cur=node 7- Cur ->next=Null 8- End 5 6 Start Cur 7Null New Node

I NSERT A T A NY POSITION 1- Start 2- Prepare Node 3- Read Data 4- Read Pos 5- Node -> Data=Data 6- temp =start 7- Let Cn=1 8- while( Cn < pos-1) 9- temp=temp-> next 10- Cn=Cn End While 12- Node -> next=temp ->next 13- temp ->next=node 14- End

I NSERT A T A NY P OSITION New Node Temp

D ELETE N ODE A T F IRST 1-Start 2- temp=Start 3- Start= Start -> next 4- Delete ( temp ) 5- temp=Null 6- End. Start New Node

D ELETE A T L AST 1- Start 2- Let Temp = Start 3- While ( temp ->next <> Cur) 4- temp=temp ->next 5- End While 6- Delete (Cur) 7- Cur=temp 8-Cur->next=null; 9- temp=null 10- End.

D ELETE A T A NY P OSITION 1- Start 2- Let Temp=start 3- Read Pos 4- Cn=1 5- while ( Cn< pos – 1 ) 6- temp=temp -> next 7- Cn=Cn+1 8- End While 9- p= temp -> next 10- temp->next=temp ->next -> next 11- Delete (p) 12- p=null 13- end

T RAVERSING 1- Start 2- temp=start 3- While ( temp <> null ) 4- print (temp -> Data ) 5- temp = temp -> next 6- End while 7- End.

S EARCHING 1- Start 2- let temp=start 3- Read key 4- While ( temp <> null) 5- If ( temp -> Data = key ) then 6- print ( “ Found “) 7- else Print ( “Not Found “ ) 8- temp = temp -> next ) 9- End While 10 - End

A SSIGNMENT Write Algorithm to implement Stack using SLL Write Algorithm to Implement Queue Using SLL Write Algorithm to Sorting SLL