Linked Lists: Data and File Structures Laboratory
Linked Lists: Data and File Structures Laboratory
http://www.isical.ac.in/~dfslab/2018/index.html
typedef struct {
...
} DATA;
NODE *create_node(DATA d) {
NODE *nptr;
if (NULL == (nptr = Malloc(1, NODE))) /* see common.h for
definitions */
ERR_MESG("out of memory"); /* of macros
*/
nptr->data = d;
nptr->next = NULL;
return nptr;
}
Functions:
create node()
insert()
insert at beginning / end
insert in front of given node
insert after given node
delete()
delete from beginning / end
delete given node
typedef struct {
...
} DATA;
typedef struct {
DATA data;
int next;
} NODE;
typedef struct {
int head, free;
int length, size;
NODE *elements;
} LIST;
typedef struct {
... DATA next
} DATA; elements
free
typedef struct {
0 1
DATA data;
int next; 1 2
} NODE;
typedef struct { 2 3
int head, free;
int length, size;
NODE *elements;
3 4 size
} LIST;
n−1
LIST create_list(int n) {
int i;
LIST l;
if (NULL ==
(l.elements = Malloc(n,
NODE)))
ERR_MESG("out of memory");
for (i = 0; i < n-1; i++)
l.elements[i].next = i+1;
l.elements[n-1].next = -1;
l.size = n;
l.free = 0;
l.head = -1;
l.length = 0;
return l;
}
LIST create_list(int n) {
int i;
LIST l; void insert(LIST *l, DATA *d, int *node)
if (NULL == { /* insert d in front of node */
(l.elements = Malloc(n, int position = l->free;
NODE))) if (-1 == position)
ERR_MESG("out of memory"); // no space left; what to do??
for (i = 0; i < n-1; i++) l->free = l->elements[l->free].next;
l.elements[i].next = i+1; l->elements[position].data = *d;
l.elements[n-1].next = -1; l->elements[position].next = *node;
l.size = n; *node = position;
l.free = 0; l->length++;
l.head = -1; }
l.length = 0; }
return l;
}
Example:
Generated elements: 10, 3, 7, 1, . . .
List: 10 → 3 10 → 3 7 10 → 1 3 7 10
Run your program 5 times each for N = 100, 500, 1000, 2000, 3000,
. . ., 10000. Print the sorted list to standard output, and the time taken
(followed by a single tab, but no newline) to standard error. Find the
average time taken for each value of N and for each implementation
method given above. You may use the shell script given below.
2. Modify your program above so that it generates two sorted lists
instead of one. Write a function to merge these two lists into a single
sorted list. For this problem, use traditional linked lists only.
3. Write a program that takes a linked list of linked lists, and creates a
single flattened linked list, as shown in the example below.
Input Output
5 → 10 →19 →28 5 → 7 → 8 → 30 → 10 → 20 → 19 → 22 → 50
→ 28 → 35 → 40 → 45
↓ ↓ ↓ ↓
7 20 22 35
Input file format:
↓ ↓ ↓
4 # Number of lists
8 50 40 5 7 8 30 # List 1
10 20 # List 2
↓ ↓ 19 22 50 # List 3
28 35 40 45 # List 4
30 45