Presentation is loading. Please wait.

Presentation is loading. Please wait.

Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked.

Similar presentations


Presentation on theme: "Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked."— Presentation transcript:

1 Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked lists is that we do not need to indicate the number of values in our structure at any one time. We can simply allocate and deallocate the nodes as required. For example, a basic linked list would contain two values, the value to store and the value of the pointer. To define a node for a linked list we declare a structure to hold the values that are required. In this case two values are needed for each node, an integer and a pointer.

2 Thus, to define a node for a linked list we declare a structure to hold the values that are required. In this case two values are needed for each node, an integer and a pointer. For example, struct listnode { int value;//integer variable list node *next; //pointervariable }; Once the structure is defined we also need to define a pointer to the structure so that we may use it to access the structure. Typedef listnode *listnodepointer; We use this pointer to point to the structure namely; Listnodepointer head; Head = new listnode; (*head).value = 5; (*head).next = NULL

3 Here the pointer head is declared first which indicates the start of the list. By referencing (*head).value, we are instructing the computer to go to memory address of head and then add the offset for the value (in the first instance 0), and then store the value 5 at that address. By referencing (*head).next, we are accessing the next member in the list. A shorthand is provided to access lists so that for example: (*head).value = 5 is equivalent to head->value = 5 for example, let us create 3 nodes listnodepointer head; listnodepointer second; listnodepointer third; head-> next = second; second -> next = third; third -> next = NULL

4 If we wish to insert values into nodes we perform the following: head-> value = 1; second -> value = 2; third -> value = 3; The contents of the list would thus be as depicted in the diagram.

5 Linked List summary In summery therefore Linked List is a one dimensional data structure, each element contains data and a pointer to the next element. A NULL pointer indicates the end of the list. Doubly linked lists also contain pointers to the previous element in the list enabling recovery from link failure. Main Application areas are in data bases. struct LL { char nodename[10]; int ID; LL *nextLL, *prevLL; };

6 Assignment example Linked List and Dynamic allocation #define RECORDS 6 void main() { struct computer { char computer_name[25]; /* The computer_name */ char computer_type[25]; /* The type of computer */ int cpu_speed; /* The computers cpu_speed */ struct computer *next; /*pointer to another record of this type */ } *point, *start, *prior; /* this defines 3 pointers */ int index; start = (struct computer *)malloc(sizeof(struct computer)); strcpy(start->computer_name,"Linux 1"); strcpy(start->computer_type,"Samba server computer_type"); start->cpu_speed = 4; start->next = NULL; prior = start;

7 Malloc function The malloc function allocates space for example: start = (struct computer *)malloc(sizeof(struct computer)); This statement assigns something to the pointer start which will create a dynamic structure containing three variables. The malloc() function, by default, will allocate a piece of memory on a heap that is “ sizeof ” characters in length and will be of type character. The " sizeof " must be specified as the only argument to the function. The malloc() function returns a pointer to the allocated memory After the dynamically allocated data has been used the memory is deallocated using the free command.

8 Good programming practice dictates that we free up the dynamically allocated space before we quit. point = start; /* first block of group */ do { prior = point->next; /*next block of data */ free(point); /* free present block */ point = prior; /* point to next */ } while (prior != NULL); /* quit when next is NULL */ }

9 heap and segmentation The original IBM PC had a data bus which was 16 bits wide. As a result these computers use a microprocessor with a 64K segment size (2 16 =64K), and they require special calls to use data outside of a single segment. One limitation placed on users by most MS-DOS C compilers is a limit of 64K for the executable code if you happen to be in the small memory model. A heap is an area outside of this 64K boundary which can be accessed by the program to store data and variables. Thus the heap is outside the segment that the code is in and access to the heap is through an inter-segment jump. The data and variables are put on the heap by the system as calls to malloc() are made. The system keeps track of where the data is stored.


Download ppt "Linked Lists Chained nodes of information create what are called linked lists, with each node providing a link to the next node. A useful feature of linked."

Similar presentations


Ads by Google