















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
An introduction to data structures and algorithms, focusing on elementary concepts and implementations. Topics covered include the importance of data structures and algorithms, arrays, pointers, linked lists, stacks, queues, recursion, and various data structures for representing segments, polygons, and surfaces in 3d. The document also discusses the differences between array and linked list implementations, as well as object-oriented representations of polygon lists.
Typology: Study notes
1 / 55
This page cannot be seen from the preview
Don't miss anything!
















































Elementary data structures
Computational geometry
Scientific codes are usually designed to solve largecomplex problems representing natural phenomena
This requires: •^
Code modularization & maintenance
-^
Logical organization of the data
-^
Fast procedures to solve the problem
EXE = run OBJ = main.o file.o
calc.o
LIB
= -llapack –lblas –lm –lgfortran
CMP = gcc –g –c --pedantic LNK = gcc $(EXE): $(OBJ)
$(LNK) –o $@ $(OBJ) $(LIB)
clean:
rm –f *.o
%.o: %.c
$(CMP) $?
The speed of a code is affected by: •^
Memory access^ – Cache vs main memory: contiguous data^ – Graphic memory vs main memory^ – Virtual memory: the end of the game
-^
Operation count^ – The total number of operations^ – Integer vs floating point operations^ – Sum / multiplications / divisions / square roots / sin-cos-exp
-^
File I/O^ – Ascii vs binary
-^
Communications^ – Shared memory vs distributed memory
Data structure are containers for storing data into thecomputer’s memory in an organized manner
-^
Data structures allow logical and efficient archival andretrieval of data items
Algorithms are procedures to perform a specific task
-^
Data structures usually have associated algorithms forefficient access to the data stored in the data structures
Arrays
-^
Linked lists
-^
Stacks
-^
Queues
-^
Hashes
An array is a fixed number of data items that are storedcontiguously and that are accessible by an index
-^
Arrays are defined as primitive in most programminglanguages (C, C++, Fortran, …)
-^
Arrays can be statically (size given when declaring thevariable) or dynamically allocated (malloc…)
-^
We refer to the ith element of an array a as: C/C++:
a[i]
Fortran:
a(i)
a
0
1
2
3
4
5
6
7
double a[100];double ptr;ptr=a;ptr=&a[0];ptr=malloc(100sizeof(double));ptr+=10;ptr=&a[10];a[10]=5.0;(ptr+10)=5.0;ptr[10]=5.0;*
A pointer is a variable that contains a memory location
-^
A pointer can be made to point to different positions inthe computer’s memory
-^
Through a pointer one can access the data stored at thelocation pointed to by the pointer
(^0) a
1
2
3
4
5
6
7
ptr
The pointer arithmetics takes into account the type ofdata being pointed to (jumps in steps of sizeof(*ptr) )
-^
Pointers can be ‘cast’ to access the memory in a different manner: float
*fptr;
char
*cptr;
cptr=(char)*
dptr;
0
1
2
3
4
5
6
7
fptr
float 1
float 2
Linked lists are defined as a primitive in some languages(Lisp, Logo) but not in most commonly used languages
-^
Main advantages of linked lists:^ – Dynamic data structure: can grow and shrink in size^ – Flexible in allowing items to be rearranged efficiently
-^
Disadvantages:^ – Slower than arrays^ – Need to traverse the list to find an item
Linked list with dummy nodes
-^
Adding an item
-^
Deleting an item
A^
B^
C^
D^
E
head
tail
A^
B^
C^
D^
E
head
tail
X
Delete link:
A^
B^
C^
D^
E
head
tail
X
Add links:
A^
B^
C^
D^
E
head
tail
X
Deletion operation: *void deleteNext(struct node n) {
n->next=n->next->next; } Insertion operation: **nodeT insertAfter(int item,struct node n) {
nodeT x=(nodeT)malloc(sizeof(x)); x->item=item; x->next=n->next; n->next=x; return x; }*
A^
B^
C^
D^
E
head
tail
X
A^
B^
C^
D^
E
head
tail
X
A^
B^
C^
D^
E
head
tail
X
Data structure: int
item[max+2],
next[max+2];
//
parallel
arrays
int
head=0,
tail=1,
x=2;
//
x:
next
unused
position
Initialization function: void
init()
{
next[head]=tail;
next[tail]=tail;
}