Docsity

Data Structure - Linked List - Notes, Study notes of Data Structures and Algorithms

Toggle sidebar
bg1
Linked List
A linked list consists of two data types, of
which first is :
•Node: The type for the nodes which will
make up the body of the list.
–Each node contains a single client data
element and a pointer to the next node in the
list. Type: struct node
struct node {
int data;
struct node* next;
};
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18

Partial preview of the text

Download Data Structure - Linked List - Notes and more Study notes Data Structures and Algorithms in PDF only on Docsity!

A linked list consists of two data types, of

which first is :

  • Node: The type for the nodes which will

make up the body of the list.

  • (^) Each node contains a single client data element and a pointer to the next node in the list. Type: struct node struct node { int data; struct node* next; };

And the second is:

  • (^) Node Pointer: The type for pointers to nodes. This will be the type of the head pointer and the “.next” fields inside each node. In C and C++, no separate type declaration is required since the pointer type is just the node type followed by a ''. Type: struct node

Linked List Example

head->data = 1; // setup first node head->next = second; // note: pointer assignment rule second->data = 2; // setup second node second->next = third; third->data = 3; // setup third link third->next = NULL; // At this point, the linked list referenced by "head" // matches the list in the drawing. return head; } This function demonstrates how calls to malloc() and pointer assignments (=) work to build a pointer structure in the heap.

Linked List Example

Building Linked List

  • (^) The best solution will be an independent function which adds a single new node to any list.
  • (^) Before getting into the specific code, we can identify the classic 3-Step Link In operation which adds a single node to the front of a linked list. The 3 steps are... 1. Allocate Allocate the new node in the heap and set its .data to whatever needs to be stored. struct node* newNode; newNode = malloc(sizeof(struct node)); newNode->data = data_client_wants_stored;

2. Link Next Set the .next pointer of the

new node to point to the current first node

of the list. This is actually just a pointer

assignment — remember: "assigning one

pointer to another makes them point to

the same thing."

newNode->next = head;

3. Link Head Change the head pointer to

point to the new node, so it is now the first

node in the list.

head = newNode;

3-Step Link In Drawing

Wrong Push() function void WrongPush(struct node* head, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = head; head = newNode; // NO this line does not work! } void WrongPushTest() { List head = BuildTwoThree(); WrongPush(head, 1); }

  • (^) We need Push() to be able to change some of the caller's memory — namely the head variable. The traditional method to allow a function to change its caller's memory is to pass a pointer to the caller's memory instead of a copy.
  • (^) To change an int in the caller, pass a int* instead. To change a struct fraction, pass a struct fraction* intead. To change an X, pass an X. So in this case, the value we want to change is struct node, so we pass a struct node** instead.
  • (^) The two stars () are a little scary, but really it's just a straight application of the rule. It just happens that the value we want to change already has one star (), so the parameter to change it has two (*).
  • (^) Or put another way: the type of the head pointer is "pointer to a struct node." In order to change that pointer, we need to pass a pointer to it, which will be a "pointer to a pointer to a struct node".

void Push(struct node** headRef, int data) { struct node* newNode = malloc(sizeof(struct node)); newNode->data = data; newNode->next = headRef; // The '' to dereferences back to the real head headRef = newNode; // ditto } void PushTest() { struct node head = BuildTwoThree();// suppose this returns the list {2, 3} Push(&head, 1); // note the & Push(&head, 13); // head is now the list {13, 1, 2, 3} }

Length() Function

/* Given a linked list head pointer, compute and return the number of nodes in the list. / int Length(struct node head) { struct node* current = head; int count = 0; while (current != NULL) { count++; current = current->next; } return count; }

Pass The List By Passing The Head Pointer

  • (^) The pointer is copied from the caller into the "head" variable local to Length().
  • (^) Copying this pointer does not duplicate the whole list. It only copies the pointer so that the caller and Length() both have pointers to the same list structure.
  • (^) This is the classic "sharing" feature of pointer code. Both the caller and length have copies of the head pointer, but they share the pointee node structure.