Data Structures Lecture 1 Goal of the course























- Slides: 23

Data Structures Lecture 1

Goal of the course �Be able to make good design choices as a developer, project manager, etc. �Be able to justify and communicate your design decisions

Program = Data structure + Algorithm

Data Structure �A data structure is a way to organize information to enable efficient computation over that information �A data structure supports certain operations, each with a: • Meaning: what does the operation do/return • Performance: how efficient is the operation

Trade offs �A data structure strives to provide many useful, efficient operations But there are unavoidable trade-offs: • Time vs. space • One operation more efficient, another less efficient • Generality vs. simplicity vs. performance �We ask ourselves questions like: • Does this support the operations I need efficiently? • Will it be easy to use (and reuse), implement, and debug?

Terminology � Abstract Data Type (ADT) • Mathematical description of a “thing” with set of operations • Not concerned with implementation details � Algorithm • A high level, language-independent description of a step-by-step process � Data structure • A specific organization of data and family of algorithms for implementing an ADT � Implementation of a data structure • A specific implementation in a specific language

Linked List

What is a Singly-linked list? �A singly linked list is a dynamic data structure consisting of a sequence of nodes, forming a linear ordering. �Each node stores: • Element (data) • Reference/pointer (i. e. , address) to the next node �Linked lists are used to implement many important data structures such as stacks, queues, graphs, hash tables, etc

Singly-linked lists vs. 1 D-arrays ID-array Fixed /static size Singly-linked list Dynamic size Insertions and Deletions are inefficient: Elements Insertions and Deletions are efficient: No shifting are usually shifted Random access i. e. , efficient indexing No random access Not suitable for operations requiring accessing elements by index such as sorting No memory waste if the array is full or almost Extra storage needed for references; however full; otherwise may result in much memory waste. uses exactly as much memory as it needs Sequential access is faster because of greater locality of references [Reason: Elements in contiguous memory locations] Sequential access is slow because of low locality of references [Reason: Elements not in contiguous memory locations]

Pointers Reminder �Pointers • • in C: int *p, *q; *p = 30; q = p 1; *q = 70 p 30 q p 70 q

Representation �struct Node { int data; struct Node *next; };

Representation

Initialize linked list �create new empty linked list �Pseudocode: Initialize(list) list = Null

Add node �Add node to the head of the linked list �Pseudocode: Add(list, x) new(P) /*creates a node and assigns pointer p to it*/ P->data = x P->Next = list List = P


Add a node • When a node is added at the beginning, – Only one next pointer needs to be modified. • list is made to point to the new node. • New node points to the previously first element. • When a node is added at the end, – Two next pointers need to be modified. • Last node now points to the new node. • New node points to NULL. • When a node is added in the middle, – Two next pointers need to be modified. • Previous node now points to the new node. • New node points to the next node.

Traverse linked list �Pseudocode: Traverse(list) P = list while (p != NULL) display(p->data) p = p->next

Traverse linked list �Once the linked list has been constructed and list points to the first node of the list, • Follow the pointers. • Display the contents of the nodes as they are traversed. • Stop when the next pointer points to NULL.

Search Linked List �Searching a linked list for a target value �Pseudocode: Search(list, x, found) P = list While(P!=NULL) and (P->data != x) P = P->Next If(P==NULL) Found = False Else Found = True

Search Linked List TARGET VALUE FOUND TARGET VALUE NOT FOUND

Delete node � � Delete a node with a target value Pseudocode: Delete(list, x, Done) q=p=list While((p!=NULL) and (p->data != x)) q=p p=p->Next If(p==NULL) Done = False Else Done = True q->Next = p->Next If(list==p) List=list->Next Free(p)

Delete node NODE IN THE MIDDLE FIRST NODE

Delete node NODE NOT FOUND