Download new28

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
no text concepts found
Transcript
Dynamic Allocation Review
Structure and list processing
Lectures 18 & 19
26.3.2001. and 27.3.2001
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
1
Dynamic allocation: review

Variables in C are allocated in one of 3 spots:
the run-time stack : variables declared local to
functions are allocated during execution
 the global data section : Global variables are
allocated here and are accessible by all parts of a
program.
 the heap : Dynamically allocated data items
malloc, calloc, realloc manage the heap region of the
mmory. If the allocation is not successful a NULL
value is returned.


26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
2
Bad Pointers




When a pointer is first allocated, it does not have a
pointee.
The pointer is uninitialized or bad.
A dereference operation on a bad pointer is a serious
runtime error.
Each pointer must be assigned a pointee before it
can support dereference operations.


int * numPtr;
Every pointer starts out with a bad value. Correct
code overwrites the bad value.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
3
Example pointer code.
int * numPtr;
int num = 42;
numPtr = #
num = malloc (sizeof (int));
*num = 73;
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
4
int a=1, b=2, c=3;
int *p, *q;
a
1
b
2
xxx p
xxx q
c
26-27.3.2001.
3
Sudeshna Sarkar, CSE, IIT
Kharagpur
5
p = &a ;
q = &b ;
a
1
b
2
p
q
c
26-27.3.2001.
3
Sudeshna Sarkar, CSE, IIT
Kharagpur
6
c = *p ;
p=q;
*p = 13 ;
a
1
p
b 13
q
c
26-27.3.2001.
1
Sudeshna Sarkar, CSE, IIT
Kharagpur
7
Bad pointer Example
void BadPointer () {
int *p;
*p = 42;
}
int * Bad2 () {
int num, *p;
num = 42;
p = #
return p;
}
26-27.3.2001.
p
xxx
Sudeshna Sarkar, CSE, IIT
Kharagpur
X
8




A function call malloc(size) allocates a block of
mrmory in the heap and returns a pointer to the new
block. size is the integer size of the block in bytes.
Heap memory is not deallocated when the creating
function exits.
malloc generates a generic pointer to a generic data
item (void *) or NULL if it cannot fulfill the request.
Type cast the pointer returned by malloc to the type
of variable we are assigning it to.
free : takes as its parameter a pointer to an allocated
region and de-allocates memory space.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
9
Dynamic memory allocation: review
typedef struct {
int hiTemp;
int loTemp;
double precip;
} WeatherData;
main () {
int numdays;
WeatherData * days;
scanf (“%d”, &numdays) ;
days=(WeatherData *)malloc (sizeof(WeatherData)*numdays);
if (days == NULL) printf (“Insufficient memory”);
...
free (days) ;
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
10
Self-referential structures

Dynamic data structures : Structures with
pointer members that refer to the same
structure.
 Arrays and other simple variables are
allocated at block entry.
 But dynamic data structures require
storage management routine to explicitly
obtain and release memory.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
11
Why linked lists ?

A linked list is a dynamic data structure.




It can grow or shrink in size during the execution
of a program.
It can be made just as long as required.
It does not waste memory space.
Linked lists provide flexibility in allowing the
items to be rearranged efficiently.


Insert an element.
Delete an element.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
12
Self-referential structures
struct list {
int data ;
struct list * next ;
};
The pointer variable next is called a link.
Each structure is linked to a succeeding structure
by next.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
13
Pictorial representation
A structure of type struct list
data next
The pointer variable next contains either
• an address of the location in memory of the
successor list element
• or the special value NULL defined as 0.
NULL is used to denote the end of the list.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
14
struct list a, b, c;
a.data = 1;
b.data = 2;
c.data = 3;
a.next = b.next = c.next = NULL;
a
b
1
NULL
data next
26-27.3.2001.
c
2
NULL
data next
3
NULL
data next
Sudeshna Sarkar, CSE, IIT
Kharagpur
15
Chaining these together
a.next = &b;
b.next = &c;
a
b
1
data next
c
2
data next
What are the values of :
• a.next->data
• a.next->next->data
26-27.3.2001.
3
NULL
data next
2
3
Sudeshna Sarkar, CSE, IIT
Kharagpur
16
Linear Linked Lists



A head pointer addresses the first
element of the list.
Each element points at a successor
element.
The last element has a link value NULL.
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
17
Header file : list.h
#include <stdio.h>
#include <stdlib.h>
typedef char DATA;
struct list {
DATA d;
struct list * next;
};
typedef struct list ELEMENT;
typedef ELEMENT * LINK;
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
18
Storage allocation
LINK head ;
head = malloc (sizeof(ELEMENT));
head->d = ‘n’;
head->next = NULL;
creates a single element list.
head
26-27.3.2001.
n
NULL
Sudeshna Sarkar, CSE, IIT
Kharagpur
19
Storage allocation
head->next = malloc (sizeof(ELEMENT));
head->next->d = ‘e’;
head->next->next = NULL;
A second element is added.
head
26-27.3.2001.
n
e
NULL
Sudeshna Sarkar, CSE, IIT
Kharagpur
20
Storage allocation
head->next=>next = malloc (sizeof(ELEMENT));
head->next->next->d = ‘e’;
head->next->next-> = NULL;
We have a 3 element list pointed to by head.
The list ends when next has the sentinel value NULL.
head
26-27.3.2001.
n
e
Sudeshna Sarkar, CSE, IIT
Kharagpur
w
NULL
21
List operations
Create a list
 Count the elements
 Look up an element
 Concatenate two lists
 Insert an element
 Delete an element

26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
22
Produce a list from a string
(recursive version)
#include “list.h”
LINK StrToList (char s[]) {
LINK head ;
if (s[0] == ‘\0’)
return NULL ;
else {
head = malloc (sizeof(ELEMENT));
head->d = s[0];
head->next = StrToList (s+1);
return head;
}
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
23
list from a string
#include “list.h”
LINK SToL (char s[]) {
(iterative version)
LINK head = NULL, tail;
int i;
if (s[0] != ‘\0’) {
head = malloc (sizeof(ELEMENT));
head->d = s[0];
tail = head;
for (i=1; s[i] != ‘\0’; i++) {
tail->next = malloc(sizeof(ELEMENT));
tail = tail->next;
tail->d = s[i];
}
tail->next = NULL;
}
return head;
Sudeshna Sarkar, CSE, IIT
} 26-27.3.2001.
Kharagpur
24
1. A one-element list
head
A
4. after assigning NULL
?
head
tail
2. A second element is attached
head
A
?
?
A
B NULL
tai
l
tail
3. Updating the tail
head
A
26-27.3.2001.
B
tai
l
?
Sudeshna Sarkar, CSE, IIT
Kharagpur
25
/* Count a list recursively */
int count (LINK head) {
if (head == NULL)
return 0;
return 1+count(head->next);
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
26
/* Count a list iteratively */
int count (LINK head) {
int cnt = 0;
for ( ; head != NULL; head=head->next)
++cnt;
return cnt;
}
void LengthTest (char a[]) {
int len; LINK list;
list = StrToList (a);
int len = count (list);
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
27
Stack
Heap
LengthTest()
a
list
len 98790
n
e
w
NULL
count ()
cnt
head
0
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
28
Stack
Heap
LengthTest()
a
list
len 98790
n
e
w
NULL
count ()
cnt
head
1
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
29
Stack
Heap
LengthTest()
a
list
len 98790
n
e
w
NULL
count ()
cnt
head
2
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
30
Stack
Heap
LengthTest()
a
list
len 98790
n
e
w
NULL
count ()
cnt
head
3
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
31
/* Print a List */
void PrintList (LINK head) {
if (head == NULL)
printf (“NULL”) ;
else {
printf (“%c --> “, head->d) ;
PrintList (head->next);
}
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
32
/* Concatenate two Lists */
void concatenate (LINK ahead, LINK bhead) {
if (ahead->next == NULL)
ahead->next = bhead ;
else
concatenate (ahead->next, bhead);
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
33
Insertion

Insertion in a list takes a fixed amount of
time once the position in the list is found.
Before Insertion
p2
p1
C
A
q
B
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
34
Insertion
/* Inserting an element in a linked list. */
void insert (LINK p1, LINK p2, LINK q) {
p1->next = q;
q->next = p2;
}
After Insertion
p2
p1
C
A
q
B
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
35
Deletion
Before deletion
p
1
2
3
p->next = p->next->next;
p
After deletion
1
26-27.3.2001.
garbage
2
Sudeshna Sarkar, CSE, IIT
Kharagpur
3
36
Deletion (free memory)
Before deletion
p
1
2
3
q = p->next;
p->next = p->next->next;
p
After deletion
1
2
q
26-27.3.2001.
3
free (q) ;
Sudeshna Sarkar, CSE, IIT
Kharagpur
37
Delete a list and free memory
/* Recursive deletion of a list */
void delete_list (LINK head) {
if (head != NULL) {
delete_list (head->next) ;
free (head) ; /* Release storage */
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
38
Insert an element into a sorted list
LIST insert ( LIST head, LIST newnode)
{
LIST cur ;
if ((head == NULL) || (newnode->d < head->d)) {
newnode->next = head;
return newnode;
}
cur = head;
while ((cur->next != NULL) && (cur->next->d < newnode->d))
cur = cur->next;
newnode->next = cur->next;
cur->next = newnode;
return head ;
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
39
InsertSort()
LINK InsertSort (LINK head)
{
LINK result = NULL;
LINK cur = head;
LINK next;
while (cur != NULL)
{
next = cur->next;
result = insert (result, cur) ;
cur = next;
}
return result;
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
40
Delete an element from a sorted list
LIST delete ( LIST head, DATA d)
{
LIST prev, temp, curr = head;
while ((curr != NULL) && (curr->d < d)) {
prev = curr ;
curr = curr->next;
}
if (curr->d == d) {
temp = curr ;
prev->next = curr->next;
free (temp);
}
return head ;
}
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
41
Assignment:Reverse a list
head
1
2
3
4
head
1
26-27.3.2001.
2
3
Sudeshna Sarkar, CSE, IIT
Kharagpur
4
42
Doubly linked list
A
B
C
Assignment :
Insertion, deletion in a doubly
linked list
26-27.3.2001.
Sudeshna Sarkar, CSE, IIT
Kharagpur
43
Related documents