23_Data_Structures - 23 Data Structures Motivation A data...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
23 - Data Structures - 1 23 - Data Structures Motivation A data structure is a complicated structure which stores information about many objects and the relationships between them. It is based on multi-field structures (which we saw in the previous chapter) and pointers to other structures. They are often adapted to fast storage and retrieval of particular kinds of data. For example, stress, heat flow and electrical analysis of solid materials often use irregular meshes which are stored as trees or graphs . These are both standard data structures that we will talk about. Linked Lists Linked lists are a simple type of data structure using pointers. Each element in the list has a value or key which is the datum that you want to store and a pointer which points to the next item in the list. They are useful because, unlike a normal array, they support a delete operation. delete allows you to remove an element from the middle of a list, with the elements to the right of it moving one left to "fill in the gap". Linked lists are based on a structure with two fields: a value field, and the next pointer. The list is an array of structures each of which represent one element of the list. To draw a linked list called onecell whose values are [20 31 45], we can use box- and-pointer notation like this: To make onecell , you would type in the following commands: >> onecell(5).value=20; >> onecell(5).next=6; >> onecell(6).value=31; >> onecell(6).next=2; >> onecell(2).value=45; >> onecell(2).next=0; If you were looking at the datum in onecell(6) and wanted to see the next datum in the list, you would type: next: 6 value: 20 next: 2 value: 31 next: 0 value: 45 onecell(5) onecell(6) onecell(2)
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
23 - Data Structures - 2 >> onecell(onecell(6).next).value ans = 45 Notice that the sequence of cells doesn’t have to have consecutive indices. That is, onecell(6) follows onecell(5), but onecell(2) is after onecell(6). The root of this linked list is onecell(5). The zero in the next field of onecell(2) indicates that it has no successor, so it’s the end of the list. In order to make a linked list, you need to know two things: the structure array, and the index of the root of the list. Printing a linked list Probably the most important thing to be able to do to a list is to print it out in order. Consider the following list: This is the function which will print out the list in order when called as printlist(onecell,1) : function printlist(list,idx) % Usage: printlist(list,idx) % % Print out a link list in order starting at a given index % % Inputs: list -- structured array of the linked list % idx -- starting index for printing % % Outputs: Prints list to screen % while idx ~= 0 disp(sprintf('index = %d \t value = %g',idx,list(idx).value)); idx = list(idx).next; end next:4 value: 10 next: 0 value: 7 next:2 value: 8 onecell(1) onecell(2) onecell(3) next:3 value: 9 onecell(4)
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 02/12/2011 for the course E 7 taught by Professor Patzek during the Spring '08 term at Berkeley.

Page1 / 10

23_Data_Structures - 23 Data Structures Motivation A data...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online