Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Understanding Data Structures: Arrays, Static & Dynamic Structures, Linked Lists, Study notes of Computer Science

An introduction to data structures, focusing on arrays, static and dynamic data structures, and linked lists. It covers the representation of data structures in c, static vs. Dynamic allocation, and the use of pointers for dynamic memory allocation. The document also discusses the advantages of linked lists and their applications in searching and sorting.

Typology: Study notes

Pre 2010

Uploaded on 11/08/2009

koofers-user-0rs
koofers-user-0rs 🇺🇸

10 documents

1 / 11

Toggle sidebar

Related documents


Partial preview of the text

Download Understanding Data Structures: Arrays, Static & Dynamic Structures, Linked Lists and more Study notes Computer Science in PDF only on Docsity! Introduction Computer algorithms are used to solve problems where the computer is one of the tools in the problem solvers arsenal. As we have mentioned many times, the success or failure of our algorithm will in part depend on how well we can represent the data that is required to solve our problem. If the data is inefficiently or unnaturally represented, then it will be very difficult for our algorithm to overcome this and be an efficient solution to the problem. Many of the constructs that we have seen in the C language are designed to allow us to better represent the data for our problem. Arrays for example, allow us to group together into a single structure, data which is similar in nature and related in some ways to each other. Similarly, the concept of the record allowed us to group heterogeneous data together in very complex fashion to help us model the data for our specific problem. For many problems, the representation of the data is best if it is modeled in certain conventional fashion. These are classic data structures that can be used to solve an incredibly wide array of problems through the representation of the data for that problem. Lists, stacks, queues, and trees, to name but a few of these data structures can be used to solve vastly different types of problems, but they are so fundamental to problem solving that even solutions to very complex problems will still take advantage of the representation of data that is provided by these structures. The remainder of our course will focus primarily on these various data structures. We will examine how the structure is represented in C, the types of problems to which the structure is suited and algorithms which allow us access to the data contained in the structure. Static vs. Dynamic Data Structures Most modern programming languages provide the built in type of an array. An array is a data structure which is (in most languages) statically allocated. This means that the space required for the array that you specify in your program is set aside before the program begins execution (during compilation). Once the program is in execution you cannot change the size of the array, it can neither become smaller nor grow larger once execution begins. This restriction can be a problem for certain types of programs. However, there are even greater restrictions on the efficiency of arrays for certain types of problems. Consider the following situation: we have an array capable of holding 10 elements and currently the array contains 8 elements. The elements in the array are maintained in ascending order (let’s assume the elements are just numbers). Lets’ assume the array looks like the one below: Data Structures - 1 Introduction to Data Structures 2 4 6 8 10 12 14 16 Now let’s assume that we want to insert a new number into this array. If the number we want to insert is 20, then we have much less work to do than if the number we want to insert is 3. If we want to insert 20, we simply go to the last filled position in the array, go one more position and insert the 20. On the other hand, if we want to insert the 3, then we need to find the correct place for 3 to be inserted and then move every element after 3 one location to the right. What we would like to do is to be able to know, in advance, that regardless of the value of the element to be inserted into our list, that the amount of work required to perform the insertion is always the same (asymptotically the same). To do this efficiently over a wide variety of data structures requires that we deal with dynamically allocated memory. As far as C is concerned, this means that we will be dealing heavily with pointers. Self-Referential Structures  We can define structures with pointer fields that refer to the structure type containing them. These are referred to as self-referential structures.  Example: struct listnode { int data; struct listnode * next; };  The pointer variable next is called a link. Each structure is linked to a succeeding structure by way of the field next. The pointer variable next contains the address of either the location in memory of the successor struct listnode element or the special value NULL, which indicates that it points to no location in memory.  Example: struct listnode a, b, c; a.data = 1; b.data = 2; c.data = 3; a.next = b.next = c.next = NULL; This creates the following: Data Structures - 2 ptr ? Linked Lists  Linked lists are a very important data structure with applications in searching and sorting as well as many other areas.  A linked list is an abstraction of a list. Thus, it is a sequence of nodes in which each node is linked to the node following it.  Lists of data can be stored in arrays, but linked lists provide several advantages which include flexibility and adaptability. Arrays  In an array each node (element) follows the previous one physically in contiguous locations in memory.  Arrays are fixed size: either too big or too small but almost never just right!  Maximum size must be predicted in advance which in some cases is impossible.  Inserting and deletion can be difficult depending on the application. Linked Lists  Linked lists are appropriate when the number of data elements to be represented in the data structure at one time is unpredictable.  Linked lists are dynamic, so the length of a list can increase or decrease as necessary.  Each node does not necessarily follow the previous one physically in the memory.  Linked lists can be maintained in sorted order by inserting or deleting an element at the proper location without affecting other portions of the list.  Three basic operations are supported by this data structure. They are insert, delete, and retrieve.  Insert – in general, adds a node to the list. Data Structures - 5  Delete – in general, removes a node from the list.  Retrieve – in general, returns a data component from a selected node. Insertion generally involves the reallocation of memory and reference pointer manipulation to update the list. Deletion generally involves changing the reference pointers to skip over the deleted node and possibly to deallocate the memory. Regardless of the number elements in the list these operations can be performed in constant time. In many instances, the solution to a problem will require the ability to insert new nodes or delete existing nodes from arbitrary positions in the list. The stack and queue structures may be too restrictive for certain applications. For this reason, the insertion and deletion operations for the linked list structure will allow the operation to be performed at an arbitrary point in the list. Insertion 1. Insertion at the beginning of the list 2. Insertion at the end of the list 3. Insertion in the middle of the list A linked list 1. Insertion at the beginning of the list 2. Insertion at the end of the list Data Structures - 6 new 3. Insertion in the middle of the list Deletion 1. Delete the head of the list 2. Delete the tail of the list 3. Delete in the middle of the list Variations on Linked Lists The linked list structures that we have just examined are all of the same type, called a singly-linked list. Each node in the list contains a single reference (pointer) to the node which logically follows it in the list. There are many different variations of linked lists that have been developed. A few of these are: Data Structures - 7 new new To call this function: my_list = array_to_list(numbers, 4); Counting the number of nodes in a linked list Data Structures - 10 Practice: Create a recursive version of this function to create a linked list from an array of values. Answer will appear in the next set of notes. Searching for an item in a linked list Data Structures - 11 //iterative version //count the number of nodes in a linked list int count (struct node *head) { struct node *p; int c = 0; p = head; while (p != NULL) { c++; p = p->next; } return c; } Practice: Create a recursive version of this function to count the nodes in a linked list. Answer will appear in the next set of notes. //search for an item in a linked list referenced by “head” struct node * lookup(int item, struct node *head) { if (head == NULL) return NULL; else if (item == head->data) return head; else return(lookup(item, head->next)); } Practice: Trace the execution of this recursive function on an example list. Answer will appear in the next set of notes.
Docsity logo



Copyright © 2024 Ladybird Srl - Via Leonardo da Vinci 16, 10126, Torino, Italy - VAT 10816460017 - All rights reserved