Download csc215-10-data-structures-part-1

Survey
yes no Was this document useful for you?
   Thank you for your participation!

* Your assessment is very important for improving the workof artificial intelligence, which forms the content of this project

Document related concepts

Quadtree wikipedia , lookup

Red–black tree wikipedia , lookup

Lattice model (finance) wikipedia , lookup

Interval tree wikipedia , lookup

Binary tree wikipedia , lookup

B-tree wikipedia , lookup

Binary search tree wikipedia , lookup

Linked list wikipedia , lookup

Transcript
Data Structures
Part 1
Outline
● Linked Lists
● Binary Trees
● Stacks
● Queues
● Hash Tables
Linked Lists
•
A linked list is a set of dynamically allocated nodes, arranged in such a way that
each node contains one value (data) and one pointer. The pointer always points to
the next member of the list. If the pointer is NULL, then it is the last node in the
list.
•
Essentially, linked lists function as an array that can grow and shrink as needed,
from any point in the array.
•
Data is stored in a linked list dynamically—each node is created as necessary.
•
A node can contain data of any type including other struct objects.
Linked Lists Advantages
•
Linked lists are dynamic, so the length of a list can increase
or decrease as necessary.
•
A linked list is appropriate when the number of data elements
to be represented is unpredictable.
•
Arrays can become full. Linked lists become full only when
the system has insufficient memory to satisfy dynamic
storage allocation requests.
Linked Lists disadvantages
• There is no "random" access - it is impossible to
reach the nth item in the array without first iterating
over all items up until that item.
• Dynamic memory allocation and pointers are
required, which complicates the code and increases
the risk of memory leaks and segment faults.
Arrays vs. Linked lists
Types of Linked Lists
●There are two basic types of linked list:
○Singly Linked List.
○Doubly Linked List.
Singly Linked List
●Each node has only one link part.
●Each link part contains the address of the next node in the list.
●A linked list is accessed via a pointer to the first node of the list. If that pointer is
NULL, then the list is considered to be empty.
Basic List Operations
●Creating a list.
●Counting the elements.
●Looking up an element.
●Inserting an element.
●Deleting an element.
●Reversing a List.
Now Let’s Define a Linked List
Node
● Structures with pointer members that refer to the structure type containing them are
called self-referential structures.
struct node{
int val;
struct node *next;
};
● Each variable of type struct node has two members, val and next. The pointer
variable next is called link. Each structure is linked to a succeeding structure by way
of the member next.
● The pointer variable next contains an address of either the location in memory of the
successor struct node element or NULL.
Defining Nodes and Linking them
● struct node a,b,c;
a.val = 1;
b.val = 2;
a.next = &b;
c.val = 3;
b.next = &c;
b
a
1
2
● a.next -> val
“has value 2”
●a.next -> next -> val
“has value 3”
c.next = NULL;
c
3
NULL
Creating a Local Variable that points to the
first item of the list
main()
{
struct node * head = NULL;
head = (struct node*)malloc(sizeof(struct node));
if (head == NULL) {
return 1;
}
head->val = 1;
head->next = NULL;
}
we should always
check if malloc
returned a NULL
value or not.
Creating a linked list
struct node *head, *prev, *curr;
head =(struct node *) malloc (sizeof(struct node));
head -> val = 1;
prev = head;
for (i = 2; i <= n; i++){
cur = (struct node *) malloc (sizeof (struct node));
cur -> val = i;
prev -> next = cur;
prev = cur;
}
prev -> next = NULL;
Inserting a node in a SLL
●There are three cases here:
○Insertion at the beginning.
○Insertion at the end.
○Insertion after a particular node.
Inserting a node at the beginning
of the list
To add a node to the beginning of the list, we will need to do the following:
• Create a new item and set its value.
• Link the new item to point to the head of the list.
• Set the head of the list to be our new item.
• If the list is empty, simply make the start pointer (head) point towards the new node.
Inserting a node at the beginning
of the list
void insertAtBeginnig(struct node *head, int val){
struct node * newNode = (struct node *) malloc (sizeof (struct node));
newNode -> val = val;
newNode -> next =head;
head = newNode;
}
Inserting a node at the end of the
list
Here, we need to iterate over all the members of the linked list:
•
•
•
we use a pointer called current.
We set it to start from the head.
In each step, we advance the pointer to the next item in the list, until we reach the
last item.
Inserting a node at the end of the
list
void insertAtEnd(struct node *head, int val){
struct node * current = head;
while (current->next != NULL) {
current = current->next;
}
/* now we can add a new variable */
current->next = (struct node *) malloc(sizeof(struct node));
current->next->val = val;
current->next->next = NULL;
}
Inserting a node after an element
Here, we need to do the following:
● Make the next pointer of the node to be inserted point to the next node of the node
after which you want to insert the node.
● Make the next pointer of the node after which the node is to be inserted, point to the
node to be inserted.
Iterating Over a List
●Let's build a function that prints out all the items of a
list. To do this:
• we need to use a current pointer that will keep track
of the node we are currently printing.
• After printing the value of the node, we set the
current pointer to the next node, and print again,
until we've reached the end of the list (the next node
is NULL).
Printing the linked list
void print_list(struct node * head) {
struct node * current = head;
while(current != NULL)
{
printf("%d\n",current->val);
current = current ->next;
}
Deleting a node in SSL
●Here we also have three cases:
○Deleting the first node.
○Deleting the last node.
○Deleting a specific node.
Deleting a specific node
void delete(struct node *head; int key){
if (head != NULL) {
if (head> val == key){
node * temp = head;
head = head -> next;
free(temp);
}
else {
node *prev = head;
node *cur = head -> next ;
while (cur != NULL)
{
if (cur ->val == key)
{
prev -> next = cur -> next;
free(cur);
break;
}
prev = cur;
cur = cur ->next;
}
}}
More Basic List Operations
● Insert a node at the middle of a list
● Insert a node in sorted order
● Concatenate two lists.
Common Programming Error
● When working with self-referential structures, a common programming error
is to access the wrong place in memory. For example, if a linked list is not
properly terminated by a NULL pointer, some form of unexpected run-time
error occurs.