Linked List
🔏 Linked List
: a set of ordered
items. A node consists of data and link, and the link connects nodes.
⚙ Operations in list
Object : a group of ݊n ordered elements
pos : index in list
▪ add_last(list, item)
: Add ‘item’ to the end.
▪ add_first(list, item)
: Add ‘item’ to the beginning.
▪ add(list, pos, item)
: Add ‘item’ to ‘pos’.
▪ delete(list, pos)
: Removes the element at ‘pos’.
▪ clear(list)
: Removes all elements of the list.
▪ replace(list, pos, item)
: Replace the element at ‘pos’ with ‘item’.
▪ is_in_list(list, item)
: Check to see if ‘item’ is in the list.
▪ get_entry(list, pos)
: Return the element at ‘pos’.
▪ Get_length(list)
: Return the length of the list.
▪ Is_empty(list)
: Check if the list is empty.
▪ Is_full(list)
: Check if the list is full.
▪ Display(list)
: Display all elements in the list.
structure of List
Node consists of entry
and address
Node consists of
entry
and address
Data field (entry)
- store the data value (element) Link field (address)
- stores the address (pointer) of next node Head pointer
: Variable that points to the first node
in the list▶ A node is created by allocating dynamic memory whenever necessary
🔬 application in code using array
🔴 simple Linked List
📥 Simple Linked List: Insertion
📤 Simple Linked List: Deletion
🔬 Simple Linked List Implementation
: each node in Linked list can be defined using a structure
🔬 Simple Linked List Implementation
: each node in Linked list can be defined using a structure
ListNode = data + link(pointer)
Node generation
: using 'malloc' functionSetting
data fields and link fieldsCreating a second node and
connecting
it to the first nodeInsertion function
: Since the head pointer changes
inside the function, we need apointer to the head pointer
.
🚩 insert in the middle
of list
If ‘*phead’ is not NULL, insert in the middle of list
(1) Copy ‘p’->link to ‘new_node’->link.
(2) Let p -> link point to new_node.
🔬 code
// phead: pointer to the head pointer of the list
// p: preceding node
// new_node: node to be inserted
void insert_node(ListNode **phead, ListNode *p, ListNode *new_node)
{
if( *phead == NULL ){
new_node->link = NULL;
*phead = new_node;
}
else {
if(p == NULL){
printf(“The preceding node cannot be NULL.\n”);
exit(1);
}
else{
new_node->link = p->link;
p->link = new_node;
}
}
}
Deletion function
: The link of ‘p’ is changed to point to the next node of ‘removed’.
so change p.link --> removed.list
(since removed.link = address of next node of ‘removed’)
🔬 code
// phead : pointer to the head pointer
// p: preceding node to be deleted
// removed: node to be deleted
void remove_node(ListNode **phead, ListNode *p, ListNode *removed)
{
if( *phead == NULL ){
printf(“The list is blank.\n”);
}
else {
if( p == NULL )
printf(“The preceding node cannot be NULL.\n”);
else {
p->link = removed->link;
free(removed);
}
}
}
📥📝🗓🖇
⚜🔰🔬🔭▶➡α
✒📤🧾📜📗📕📘💡💾⛏🛠🔑🔏🔊🌈🔥🚩💫❓❗🔰♻✅✔🟥🟧🟨🟩🟦🟪🔺🖼🎞🎨🎗🔑⚠❔🔀↪➰💱🔴🔴🟠🟡🟢🔵🟣🟤⚫🗨🗝🔑🔨🔐🛠🎬🕯📸🔖👾👻✔👑💎🔭🔎
Reference
この問題について(Linked List), 我々は、より多くの情報をここで見つけました
https://velog.io/@flowersayo/Linked-List-99y1i3qm
テキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol
// phead: pointer to the head pointer of the list
// p: preceding node
// new_node: node to be inserted
void insert_node(ListNode **phead, ListNode *p, ListNode *new_node)
{
if( *phead == NULL ){
new_node->link = NULL;
*phead = new_node;
}
else {
if(p == NULL){
printf(“The preceding node cannot be NULL.\n”);
exit(1);
}
else{
new_node->link = p->link;
p->link = new_node;
}
}
}
: The link of ‘p’ is changed to point to the next node of ‘removed’.
so change p.link --> removed.list
(since removed.link = address of next node of ‘removed’)
🔬 code
// phead : pointer to the head pointer
// p: preceding node to be deleted
// removed: node to be deleted
void remove_node(ListNode **phead, ListNode *p, ListNode *removed)
{
if( *phead == NULL ){
printf(“The list is blank.\n”);
}
else {
if( p == NULL )
printf(“The preceding node cannot be NULL.\n”);
else {
p->link = removed->link;
free(removed);
}
}
}
📥📝🗓🖇⚜🔰🔬🔭▶➡α
✒📤🧾📜📗📕📘💡💾⛏🛠🔑🔏🔊🌈🔥🚩💫❓❗🔰♻✅✔🟥🟧🟨🟩🟦🟪🔺🖼🎞🎨🎗🔑⚠❔🔀↪➰💱🔴🔴🟠🟡🟢🔵🟣🟤⚫🗨🗝🔑🔨🔐🛠🎬🕯📸🔖👾👻✔👑💎🔭🔎
Reference
この問題について(Linked List), 我々は、より多くの情報をここで見つけました https://velog.io/@flowersayo/Linked-List-99y1i3qmテキストは自由に共有またはコピーできます。ただし、このドキュメントのURLは参考URLとして残しておいてください。
Collection and Share based on the CC Protocol