Module 3 Linear Data Structures and Linked Storage Representation

Total Page:16

File Type:pdf, Size:1020Kb

Module 3 Linear Data Structures and Linked Storage Representation Module 3 17CS33 Module 3 Linear data structures and linked storage representation Advantages of arrays Linear data structures such as stacks and queues can be represented and implemented using sequential allocation ie using arrays. Use of arrays have the following advantages Data accessing is faster because we just need to specify the array name and the index of the element to be accssed.. ie arrays are simple to use Disadvantages The size of the array is fixed. This limits the user in determining the required storage well in advance. However memory requirement cannot be determined while writing the program . ie if the allocation is more and required is less then memory is wasted. Similarly if less memory is allocated and requirement is more then we cannot allocate memory during execution. Also array items are stored sequentially. In such a case contiguous memory may not be available. Most of the times chunks of memory will be available but they cannot be used as they are not contiguous. Finally insertion and deletion of items in a array is difficult. Insertion of an element at the ith position requires us to move all the elements from i+1 th position To the next position and only after this movement insertion can be performed. A similar movement is required for deletion also. Linked lists A linked list is a collection of zero or more nodes where each node has some information data link Link will contain the address of the next node data will contain the information. Hence if the address of a node is known we can easily access the subsequent nodes Module 3 17CS33 Types of lists Singly linked lists doubly linked lists circular linked lists circular doubly linked lists Singly linked list A singly linked list is a collection of zero or more nodes. Each node will have one info field and one link field. A chain is a singly list with zero or more nodes. A empty list is as shown \0 A list is represented as follows 10 20 30 40 First Note: List contains 4 nodes where each node contains a info and link fields data filed contains the data Link field contains the address of the next node First is a variable containing the address of the first node of the list Using first we can access any node in the list The link field of the last node will have NULL Module 3 17CS33 Representation Nodes in the list are self referential structures. A structure is a collection of one or more fields which are of the same type or different type. A self refrential structure is a structure which has at least one field which is a pointer to the same structure . ex struct Node { int data; struct node * link; }; data is a int containing integer data Link is a pointer and should contain the address and normally it contains the address of the next node Declaration of variable We have the following declaration struct Node { Int info; Struct Node * link; }; struct Node * first; Creation of a empty list Before creating a list we need to create a empty list first. This is achieved by assigning NULL Module 3 17CS33 to the variable first ie first=NULL creating a new node A new node is created as follows first=(struct Node *) malloc( sizeof(struct Node)); The above statement will allocate memory to the variable first Using first we can access the contents of the node such as First data or first link(for the next node) To store the data. We use firstdata=10; We need to store NULL as this is the first node Firstlink=NULL; Deleting a node Any node which is not required is deleted using the free function free(first); When the above statement is executed the memory which was allocated to first using malloc is deallocated and returned to the Operating System. The variable first will have a invalid address and hence is called as dangling pointer. Operations on a singly linked list Inserting a node Deleting a node Search for a node Display the list Module 3 17CS33 #include<stdio.h> #include<stdlib.h> // Declaration of the node struct Node { int data; struct Node *link; }; struct Node first =NULL; //function to add a node at the end of the list void InsertL( int ele) { struct node *p,*q; p= (struct node *)malloc(sizeof(struct node)); p data=ele; for(q = first; q link != NULL; q=qlink) ; qlink = p; q = p; qlink=NULL; } Void InsertF( int ele ) { struct node *p; p =(struct Node *)malloc(sizeof(struct Node)); pdata=ele; plink =NULL; if (first = = NULL) first=p; else { plink=first; first = p; } } Module 3 17CS33 //function to add a node after a given node void addAfter(int ele, int loc) { int i; struct node *p,*q,*prev = NULL; q = first; for(i=1; i<loc;i++) { prev = q; q = q link; } p=(struct node *)malloc(sizeof(struct node)); pdata= ele; prevlink =p; prev = p; prev link= q; } Note: For all delete Function refer Class notes // function to display a list void display(struct Node *first) { struct Node *p; p = first; if(p = =NULL) { Printf(“List is Empty\n”); return; } while(p !=NULL) { printf("%d ",p data); p=plink; } } Module 3 17CS33 //function to count the number of nodes in a list int count(struct Node *first) { struct Node *p = first; int cnt=0; while(p!=NULL) { p=plink; cnt++; } return cnt; } Function to search for a element in a list struct Node * search(struct Node *first, int key) { struct Node *p while (p != NULL) { if (p data = = key) /* successful search */ return p; else p = p link; } return NULL; /* unsuccessful search */ } Circular linked list Circular Linked List is little more complicated linked data structure. In the circular linked list we can insert elements anywhere in the list whereas in the array we cannot insert element anywhere in the list because it is in the contiguous memory. In the circular linked list the previous element stores the address of the next element and the last element stores the address of the starting element. The elements points to each other in a circular way which forms a circular chain. The circular linked list has a dynamic size which means the memory can be allocated when it is required. Module 3 17CS33 Application of Circular Linked List The real life application where the circular linked list is used is our Personal Computers, where multiple applications are running. All the running applications are kept in a circular linked list and the OS gives a fixed time slot to all for running. The Operating System keeps on iterating over the linked list until all the applications are completed. Another example can be Multiplayer games. All the Players are kept in a Circular Linked List and the pointer keeps on moving forward as a player's chance ends. Circular Linked List can also be used to create Circular Queue. In a Queue we have to keep two pointers, FRONT and REAR in memory all the time, where as in Circular Linked List, only one pointer is required. Note: For Operations on circular Linked List refer class Notes Doubly linked list In computer science, a doubly linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Each node contains two fields, called links, that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' llink and rlink links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. If there is only one sentinel node, then the list is circularly linked via the sentinel node. It can be conceptualized as two singly linked lists formed from the same data items, but in opposite sequential orders. A doubly linked list whose nodes contain three fields: an integer value, the link to the next node, and the link to the previous node. The two node links allow traversal of the list in either direction. While adding or removing a node in a doubly linked list requires changing more links than the same operations on a singly linked list, the operations are simpler and potentially more efficient (for nodes other than first nodes) because there is no need to keep track of the previous node during traversal or no need to traverse the list to find the previous node, so that its link can be modified. Note: For Operations on doubly Linked List refer class Notes Applications of linked lists Polynomial addition A polynomial is sum of terms where each term is of the form axe Module 3 17CS33 Where a is the coefficient is the variable, e is the exponent The largest exponent is also called as the degree. Various operations can be performed on the polynomials Iszero(poly)-Returns 0 if the polynomial does not exist Coeff(poly,expo)-returns the coefficient Leadexpo(poly)-returns the largest exponent Attach- Attach a term<coeff,expo> onto the polynomial Remove (poly,expo)-delete a term Add(poly1,poly2)-adds two polynomials General procedure to add two polynomials Three cases exist Case 0 : Powers of the two terms to be added are equal In such a case the coefficients of the two terms are added using Sum=coeff(a,leadexpo(a)+coeff(b,leadexpo(b)) And then insert the resultant term on to the resulting polynomial using the attach function. We move onto the next term using the remove function. Ie A=remove(a,leadexpo(a)); B= remove (b,leadexpo(b)); Case 1: Power of the first term is > power the second term In such a case we insert the first term onto the resulting polynomial using Attach(c,coeff(a,leadexpo(a)) Module 3 17CS33 And move onto the next term using remove function A=remove(a,leadexpo(a) Default case: Here the power of the first term is < the power of the second term In such a case we insert the second term onto the resulting polynomial using Attach(c,coeff(b,leadexpo(b)) And
Recommended publications
  • Device Tree 101
    Device Tree 101 Device Tree 101 Organized in partnership with ST February 9, 2021 Thomas Petazzoni embedded Linux and kernel engineering [email protected] © Copyright 2004-2021, Bootlin. Creative Commons BY-SA 3.0 license. Corrections, suggestions, contributions and translations are welcome! - Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 1/56 Who is speaking ? I Thomas Petazzoni I Chief Technical Officer at Bootlin I Joined in 2008, employee #1 I Embedded Linux & Linux kernel engineer, open-source contributor I Author of the Device Tree for Dummies talk in 2013/2014 I Buildroot co-maintainer I Linux kernel contributor: ≈ 900 contributions I Member of Embedded Linux Conference (Europe) program committee I Based in Toulouse, France - Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 2/56 Agenda I Bootlin introduction I STM32MP1 introduction I Why the Device Tree ? I Basic Device Tree syntax I Device Tree inheritance I Device Tree specifications and bindings I Device Tree and Linux kernel drivers I Common properties and examples - Kernel, drivers and embedded Linux - Development, consulting, training and support - https://bootlin.com 3/56 Bootlin I In business since 2004 I Team based in France I Serving customers worldwide I 18% revenue from France I 44% revenue from EU except France I 38% revenue outside EU I Highly focused and recognized expertise I Embedded Linux I Linux kernel I Embedded Linux build systems I Activities
    [Show full text]
  • Slide Set 17 for ENCM 339 Fall 2015
    Slide Set 17 for ENCM 339 Fall 2015 Steve Norman, PhD, PEng Electrical & Computer Engineering Schulich School of Engineering University of Calgary Fall Term, 2015 SN's ENCM 339 Fall 2015 Slide Set 17 slide 2/46 Contents Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 3/46 Outline of Slide Set 17 Data structures Abstract data types Linked data structures A singly-linked list type in C++ Operations and implementation of SLListV1 The Big 3 for a linked-list class Ordered linked lists SN's ENCM 339 Fall 2015 Slide Set 17 slide 4/46 Data structures In discussion of programming, the term data structure means a collection of data items, organized to support some of the following goals: I fast insertion or removal of data items I fast access to data items I fast searching for the location of a particular data item, if the item is in the collection I maintaining items in some kind of sorted order I keeping the memory footprint of the collection as small as possible There are design trade-offs|no single data structure design is best for all of the above goals. SN's ENCM 339 Fall 2015 Slide Set 17 slide 5/46 Here are two kinds of data structures that everybody in this course should be familiar with: I arrays I C and C++ structure objects We're about to study a kind of data structure called a linked list.
    [Show full text]
  • Fast Sequential Summation Algorithms Using Augmented Data Structures
    Fast Sequential Summation Algorithms Using Augmented Data Structures Vadim Stadnik [email protected] Abstract This paper provides an introduction to the design of augmented data structures that offer an efficient representation of a mathematical sequence and fast sequential summation algorithms, which guarantee both logarithmic running time and logarithmic computational cost in terms of the total number of operations. In parallel summation algorithms, logarithmic running time is achieved by high linear computational cost with a linear number of processors. The important practical advantage of the fast sequential summation algorithms is that they do not require supercomputers and can run on the cheapest single processor systems. Layout 1. Motivation 2. Parallel Summation Algorithms 3. Choice of Data Structure 4. Geometric Interpretation 5. Representation of Sequence 6. Fast Sequential Summation Algorithms 7. Mean Value and Standard Deviation 8. Other Data Structures 1. Motivation The original version of the fast sequential summation algorithms described here was implemented in C++ using data structures that support interfaces of STL containers [1]. These interesting and important algorithms can be implemented in many other programming languages. This discussion provides an introduction to the design of data structures, which significantly improve performance of summation algorithms. The fast summation algorithms have been developed by applying the method of augmenting data structures [2]. This powerful method can be used to solve a wide range of problems. It helps design advanced data structures that support more efficient algorithms than basic data structures. The main disadvantage of this method is its complexity, which makes the use of this method difficult in practice.
    [Show full text]
  • Linked List Data Structure
    Linked List Data Structure Static and Dynamic data structures A static data structure is an organization of collection of data that is fixed in size. as memory ,(مسبقا") This results in the maximum size needing to be known in advance cannot be reallocated at a later. Arrays are example of static data structure. A dynamic data structure , where in with the latter the size of the structure can dynamically grow or shrink in size as needed. Linked lists are example of dynamic data structure. Linked Lists Like arrays, Linked List is a linear data structure. Unlike arrays, linked list elements are not stored at contiguous location; the elements are linked using pointers. It consists of group of nodes in a sequence which is divided in two parts. Each node consists of its own data and the address of the next node and forms a chain. Linked Lists are used to create trees and graphs. What are different between linked lists and arrays? Arrays Linked lists Have a pre-determined fixed No fixed size; grow one element at a time size Array items are store Element can be stored at any place contiguously Easy access to any element in No easy access to i-th element , need to hop through all previous constant time elements start from start Size = n x sizeof(element) Size = n x sizeof (element) + n x sizeof(reference) Insertion and deletion at Insertion and deletion are simple and faster particular position is complex(shifting) Only element store Each element must store element and pointer to next element (extra memory for storage pointer).
    [Show full text]
  • The Hitchhiker's Guide to Data Structures
    The Hitchhiker’s Guide to Data Structures University of Rochester Nathan Contino March 2, 2017 Abstract Introductory computer science students often have considerable difficulty extracting infor- mation from data structures textbooks with rather dense writing styles. These textbooks are usually intended to cover subjects comprehensively, but seldom focus on keeping content comprehensible. While this commitment to data preservation is admirable at heart, the author of this paper feels that we can do a better job for introductory students by attempting to balance comprehensibility and comprehensiveness. The Hitchhiker’s Guide to Data Structures is an attempt to present data structures to introductory computer science students in this more friendly manner. Contents 1 Forward 2 2 Introduction 2 2.1 A Note on Running Time . .3 2.2 A Note on Pointers and Values. .5 3 The Proto-Structures 6 3.1 Nodes . .6 3.2 Linked Lists . .8 3.3 Arrays . 13 3.4 Strings . 16 4 Where Things Start to Get Complicated 18 4.1 Doubly-Linked Lists . 18 4.2 Stacks . 22 4.3 Queues . 25 4.4 Trees . 28 4.5 Binary Search Trees . 33 5 Advanced Data Structures 37 5.1 AVL Trees . 37 5.2 Binary Heaps . 40 5.3 Graphs . 42 5.4 Hash Tables . 45 6 Summary 47 1 1 Forward While the purpose of this work seems simple, it has actually turned out to be rather difficult to convey. So we’ll try to illustrate it clearly here in a single brief sentence: this paper is designed specifically as a guide to data structures for keen, interested students at the introductory level of computer science.
    [Show full text]
  • Trees • Binary Trees • Traversals of Trees • Template Method Pattern • Data
    TREES • trees • binary trees • traversals of trees • template method pattern • data structures for trees Trees 1 Trees •atree represents a hierarchy - organization structure of a corporation Electronics R’Us R&D Sales Purchasing Manufacturing Domestic International TV CD Tuner Canada S. America Overseas Africa Europe Asia Australia - table of contents of a book student guide overview grading environment programming support code exams homeworks programs Trees 2 Another Example • Unix or DOS/Windows file system /user/rt/courses/ cs016/ cs252/ grades grades homeworks/ programs/ projects/ hw1 hw2 hw3 pr1 pr2 pr3 papers/ demos/ buylow sellhigh market Trees 3 Terminology • A is the root node. • B is the parent of D and E. • C is the sibling of B • D and E are the children of B. • D, E, F, G, I are external nodes, or leaves. • A, B, C, H are internal nodes. • The depth (level) of E is 2 • The height of the tree is 3. • The degree of node B is 2. A B C D E F G H I Property: (# edges) = (#nodes) − 1 Trees 4 Binary Trees • Ordered tree: the children of each node are ordered. • Binary tree: ordered tree with all internal nodes of degree 2. • Recursive definition of binary tree: •Abinary tree is either -anexternal node (leaf), or -aninternal node (the root) and two binary trees (left subtree and right subtree) Trees 5 Examples of Binary Trees • arithmetic expression + × × + 5 4 + × + 7 2 3 + 2 8 1 + 4 6 ((((3 × (1 + (4 + 6))) + (2 + 8)) × 5) + ( 4 × (7 + 2))) •river Trees 6 Properties of Binary Trees •(# external nodes ) = (# internal nodes) + 1 •(#
    [Show full text]
  • Verifying Linked Data Structure Implementations
    Verifying Linked Data Structure Implementations Karen Zee Viktor Kuncak Martin Rinard MIT CSAIL EPFL I&C MIT CSAIL Cambridge, MA Lausanne, Switzerland Cambridge, MA [email protected] viktor.kuncak@epfl.ch [email protected] Abstract equivalent conjunction of properties. By processing each conjunct separately, Jahob can use different provers to es- The Jahob program verification system leverages state of tablish different parts of the proof obligations. This is the art automated theorem provers, shape analysis, and de- possible thanks to formula approximation techniques[10] cision procedures to check that programs conform to their that create equivalent or semantically stronger formulas ac- specifications. By combining a rich specification language cepted by the specialized decision procedures. with a diverse collection of verification technologies, Jahob This paper presents our experience using the Jahob sys- makes it possible to verify complex properties of programs tem to obtain full correctness proofs for a collection of that manipulate linked data structures. We present our re- linked data structure implementations. Unlike systems that sults using Jahob to achieve full functional verification of a are designed to verify partial correctness properties, Jahob collection of linked data structures. verifies the full functional correctness of the data structure implementation. Not only do our results show that such full functional verification is feasible, the verified data struc- 1 Introduction tures and algorithms provide concrete examples that can help developers better understand how to achieve similar results in future efforts. Linked data structures such as lists, trees, graphs, and hash tables are pervasive in modern software systems. But because of phenomena such as aliasing and indirection, it 2 Example has been a challenge to develop automated reasoning sys- tems that are capable of proving important correctness prop- In this section we use a verified association list to demon- erties of such data structures.
    [Show full text]
  • 8.1 Doubly Linked List
    CMSC 132, Object-Oriented Programming II Summer 2017 Lecture 8: Lecturer: Anwar Mamat Disclaimer: These notes may be distributed outside this class only with the permission of the Instructor. 8.1 Doubly Linked List Like a singly linked list, a doubly-linked list is a linked data structure that consists of a set of sequentially linked records called nodes. Unlike a singly linked list, each node of the doubly singly list contains two fields that are references to the previous and to the next node in the sequence of nodes. The beginning and ending nodes' previous and next links, respectively, point to some kind of terminator, typically a sentinel node or null, to facilitate traversal of the list. Listing 1: Doubly Linked List Node Class 1 class Node<E>{ 2 E data; 3 Node previous; 4 Node next; 5 Node(E item){ 6 data = item; 7 } 8 } Usually Node class is nested inside the LinkedList class, and members of Node are private. 8.1.1 Create a simple linked list Now, let us create a simple linked list. 1 Node<String> n1 = new Node("Alice"); 2 Node<String> n2 = new Node("Bob"); 3 Node<String> n3 = new Node("Cathy"); 4 n1.next = n2; 5 n2.previous = n1; 6 n2.next = n3; 7 n3.previous = n2; This linked list represents this: Alice Bob Cathy 8.1.2 Display the Linked List We can display all the linked list: 8-1 8-2 Lecture 8: 1 Node<String> current = first; 2 while(current != null){ 3 System.out.println(current.data); 4 current = current.next; 5 } We can also display all the linked list in reverse order: 1 Node<String> current = tail; 2 while(current != null){ 3 System.out.println(current.data); 4 current = current.previous; 5 } 8.1.3 Insert a node Now, let us insert a node between \Bob" and \Cathy".
    [Show full text]
  • Linked Data Structures 17
    Linked Data Structures 17 17.1 NODES AND LINKED LISTS 733 Efficiency of Hash Tables 783 Nodes 733 Example: A Set Template Class 784 Linked Lists 738 Efficiency of Sets Using Linked Lists 790 Inserting a Node at the Head of a List 740 Pitfall: Losing Nodes 743 17.3 ITERATORS 791 Inserting and Removing Nodes Inside a List 743 Pointers as Iterators 792 Pitfall: Using the Assignment Operator with Dynamic Iterator Classes 792 Data Structures 747 Example: An Iterator Class 794 Searching a Linked List 747 17.4 TREES 800 Doubly Linked Lists 750 Tree Properties 801 Adding a Node to a Doubly Linked List 752 Example: A Tree Template Class 803 Deleting a Node from a Doubly Linked List 752 Example: A Generic Sorting Template Version of Linked List Tools 759 17.2 LINKED LIST APPLICATIONS 763 Example: A Stack Template Class 763 Example: A Queue Template Class 770 Tip: A Comment on Namespaces 773 Friend Classes and Similar Alternatives 774 Example: Hash Tables with Chaining 777 Chapter Summary 808 Answers to Self-Test Exercises 809 Programming Projects 818 MM17_SAVT071X_01_SE_C17.indd17_SAVT071X_01_SE_C17.indd 773131 22/8/12/8/12 33:53:53 PPMM 17 Linked Data Structures If somebody there chanced to be Who loved me in a manner true My heart would point him out to me And I would point him out to you. GILBERT AND SULLIVAN, Ruddigore Introduction A linked list is a list constructed using pointers. A linked list is not fixed in size but can grow and shrink while your program is running. A tree is another kind of data structure constructed using pointers.
    [Show full text]
  • MASTER of SCIENCE May,1995 DESIGN and IMPLEMENTATION of an EFFICIENT
    DESIGN AND IMPLEMENTATION OF AN EFFICIENT INDEX STRUCTURE AND A GUI FOR SPATIAL DATABASES USING VISUAL C++ By SUJATHA SAMADARSINI NEELAM Bachelor ofTechnology S. V. University Tirupathi, India 1986 Submitted to the Faculty of the Graduate College of the Oklahoma State University in partial fulfIllment of the requirements for the Degree of MASTER OF SCIENCE May,1995 DESIGN AND IMPLEMENTATION OF AN EFFICIENT INDEX STRUCTURE AND A GUI FOR SPATIAL DATABASES USING VISUAL C++ Thesis Approved: Thesis Adviser ii ACKNOWLEDGMENTS I wish to express my sincere and special thanks to my major advisor, Dr. Huizhu Lu, for her expert guidance, constructive supervision and friendship. I would also like to thank my committee members Dr. George and Dr. Benjamin for their encouragement, guidance and advice. My sincere thanks to Dr. Mark Gregory ofthe Department of Agriculture, OSU, for providing the data for this project. I would also like to take this opportunity to show my appreciation to my brother Mr. VCS Reddy Kummetha for his encouragement, guidance and friendship and heartfelt thanks to our friend Mr. Siva Rama Krishna Kavuturu, for his guidance and help, all through my study at Stillwater, when I am away from my family. My special appreciation goes to my husband Mr. Chinnappa Reddy Neelam, and my daughter Ms. Pranu Neelam, for all their encouragement, love and understanding. My in depth gratitude goes to my parents Mr. and Mrs. K. Rama Krishna Reddy, for their love, support and encouragement without which I wouldn't have been what lam today and also for taking care ofmy daughter, so I can complete my studies successfully.
    [Show full text]
  • Efficient Run-Time Support for Global View Programming of Linked Data Structures on Distributed Memory Parallel Systems
    Efficient Run-time Support For Global View Programming of Linked Data Structures on Distributed Memory Parallel Systems DISSERTATION Presented in Partial Fulfillment of the Requirements for the Degree Doctor of Philosophy in the Graduate School of The Ohio State University By Darrell Brian Larkins, M.S. Department of Computer Science and Engineering The Ohio State University 2010 Dissertation Committee: P. Sadayappan, Advisor Atanas Rountev Paul A. G. Sivilotti © Copyright by Darrell Brian Larkins 2010 ABSTRACT Developing high-performance parallel applications that use linked data structures on distributed-memory clusters is challenging. Many scientific applications use algo- rithms based on linked data structures like trees and graphs. These structures are especially useful in representing relationships between data which may not be known until runtime or may otherwise evolve during the course of a computation. Meth- ods such as n-body simulation, Fast Multipole Methods (FMM), and multiresolution analysis all use trees to represent a fixed space populated by a dynamic distribu- tion of elements. Other problem domains, such as data mining, use both trees and graphs to summarize large input datasets into a set of relationships that capture the information in a form that lends itself to efficient mining. This dissertation first describes a runtime system that provides a programming interface to a global address space representation of generalized distributed linked data structures, while providing scalable performance on distributed memory com- puting systems. This system, the Global Chunk Layer (GCL), provides data access primitives at the element level, but takes advantage of coarse-grained data movement to enhance locality and improve communication efficiency.
    [Show full text]
  • CSI 310: Lecture 12 Doubly Linked List Programming, Needed for Proj 5. University at Albany Computer Science Dept
    University at Albany Computer Science Dept. 1 CSI 310: Lecture 12 Doubly Linked List Programming, needed for Proj 5. University at Albany Computer Science Dept. 2 Summary..... What is a variable (synonyms: object, memory location, “cell”, “box for data”)? What is a pointer value (synonyms: address, locator. “Reference” in Java)? University at Albany Computer Science Dept. 3 class Thing { public void memberFun(){...}; .... }; Thing mything; mything = new Thing(..); //copy a Java reference(pointer!) mything.memberFun(); //call memberFun() ON the new Thing. University at Albany Computer Science Dept. 4 • C/C++/assembly/machine languages EXPOSE numerical pointer values/addresses. • Java HIDES numerical pointer values/addresses and provides variables called Java reference variables, to hold and copy Java reference values. • Java has only 2 types: (1) Primative type (roughly like C/C++’s but implementation independent) (2) Java “Reference type” which are pointers (to class objects only). (You cannot convert them to/from integers or any other type.) University at Albany Computer Science Dept. 5 Back to Proj 3-5 data structures... University at Albany Computer Science Dept. 6 A linked data structure consists of some structure type objects (variables) that contain some pointer type fields that hold addresses of structure type objects. Such data structures can be virtually unlimited in size if the objects are dynamically allocated. In Java, they are ALWAYS dynamically allocated (by new). The dynamic memory used shrinks as well as grows as needed, dynamically. University at Albany Computer Science Dept. 7 Dynamically Allocated Objects (You need pointers also known as Java “references”.) 1. Dynamic objects are not declared.
    [Show full text]