Skip to main content

Data Structures 02 - Singly Linked List

Code Display

#include <stdio.h>
#include <malloc.h>

/**
* Character linked list node structure. The key data is data.
*/
typedef struct LinkNode{
char data;
struct LinkNode *next;
} LNode, *LinkList, *NodePtr;

/**
* Initialize the linked list and create a head node.
* @return Pointer to the head node.
*/
LinkList initLinkList(){
NodePtr tempHeader = (NodePtr)malloc(sizeof(LNode));
tempHeader->data = '\0';
tempHeader->next = NULL;
return tempHeader;
}// End of initLinkList

/**
* Print the linked list.
* @param paraHeader The head node of the linked list.
*/
void printList(NodePtr paraHeader){
NodePtr p = paraHeader->next;
while (p != NULL) {
printf("%c", p->data);
p = p->next;
}// End of loop
printf("\r\n");
}// End of printList

/**
* Print the linked list and its memory information.
* @param paraHeader The head node of the linked list.
*/
void printListMemory(NodePtr paraHeader){
NodePtr p = paraHeader;
while (p != NULL) {
printf("At address %ld, data = %c, next = %ld \r\n", p, p->data, p->next);
p = p->next;
}// End of loop
printf("\r\n");
}// End of printListMemory


/**
* Append an element to the end of the linked list.
* @param paraHeader The head node of the linked list.
* @param paraChar The character to append.
*/
void appendElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;

// Step 1: Construct a new node.
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;
q->next = NULL;

// Step 2: Search to the end.
p = paraHeader;
while (p->next != NULL) {
p = p->next;
}// End of loop

// Step 3: Link the new node.
p->next = q;
}// End of appendElement

/**
* Insert an element at the specified position.
* @param paraHeader The head node of the linked list.
* @param paraChar The character to insert.
* @param paraPosition The specified position.
*/
void insertElement(NodePtr paraHeader, char paraChar, int paraPosition){
NodePtr p, q;

// Step 1: Search to the specified position.
p = paraHeader;
for (int i = 0; i < paraPosition; i ++) {
p = p->next;
if (p == NULL) {
printf("Position %d is beyond the scope of the list.", paraPosition);
return;
}// If null, return
} // End of for loop

// Step 2: Construct a new node.
q = (NodePtr)malloc(sizeof(LNode));
q->data = paraChar;

// Step 3: Insert the new node.
printf("Linking\r\n");
q->next = p->next;
p->next = q;
}// End of insertElement

/**
* Delete an element from the linked list.
* @param paraHeader The head node of the linked list.
* @param paraChar The character to delete.
*/
void deleteElement(NodePtr paraHeader, char paraChar){
NodePtr p, q;
p = paraHeader;
while ((p->next != NULL) && (p->next->data != paraChar)){
p = p->next;
}// End of loop

if (p->next == NULL) {
printf("Cannot delete character %c\r\n", paraChar);
return;
}// If null, return

q = p->next;
p->next = p->next->next;
free(q);
}// End of deleteElement

/**
* Unit test.
*/
void appendInsertDeleteTest(){
// Step 1: Initialize an empty linked list.
LinkList tempList = initLinkList();
printList(tempList);

// Step 2: Append some characters.
appendElement(tempList, 'H');
appendElement(tempList, 'e');
appendElement(tempList, 'l');
appendElement(tempList, 'l');
appendElement(tempList, 'o');
appendElement(tempList, '!');
printf("After appending elements");
printList(tempList);
printListMemory(tempList);

// Step 3: Delete some characters (first occurrence).
deleteElement(tempList, 'e');
deleteElement(tempList, 'a');
deleteElement(tempList, 'o');
printf("After deleting elements");
printList(tempList);
printListMemory(tempList);

// Step 4: Insert at the specified position.
insertElement(tempList, 'o', 1);
printf("After inserting element");
printList(tempList);
printListMemory(tempList);
}// End of appendInsertDeleteTest

/**
* Address test: a test beyond the textbook content.
*/
void basicAddressTest(){
LNode tempNode1, tempNode2;

tempNode1.data = 4;
tempNode1.next = NULL;

tempNode2.data = 6;
tempNode2.next = NULL;

printf("The first node: %d, %d, %d\r\n",
&tempNode1, &tempNode1.data, &tempNode1.next);
printf("The second node: %d, %d, %d\r\n",
&tempNode2, &tempNode2.data, &tempNode2.next);

tempNode1.next = &tempNode2;
}// End of basicAddressTest

/**
* Main function entry point.
*/
int main(){
appendInsertDeleteTest();
return 0;
}// End of main



Running Result