Code Implementation of Link list
1 Construction of link list
1.1 Construct a link list
typedef struct Link{
char elem; // Represents the data field
struct Link * next; // Represents the pointer field, point to the subsequent element
}link; // link is the name of the node (every node is a 'link' struct
1.2 Initialize the link list
Here we introduce two ways to initialize the link list, we can use a list with a head node or without a head node. Assume that we need to store “1,2,3,4” in the link list, here two ways.
Without a head node:
link * initLink(){
link * p=NULL; // Create the head pointer
link * temp = (link*)malloc(sizeof(link)); // Create the first node
temp->elem = 1; // Initialize the first pointer
temp->next = NULL;
p = temp; // The head pointer point to the first node
for (int i=2; i<5; i++) {
link *a=(link*)malloc(sizeof(link)); // Create and initialize each node
a->elem=i;
a->next=NULL;
temp->next=a; // Construct connection
temp=temp->next; // Move the pointer
}
return p; // Return the head pointer
}
With a head node:
link * initLink1(){
link * p=(link*)malloc(sizeof(link)); // Create a head node
link * temp=p; // A pointer point to the head node
for (int i=1; i<5; i++) { // Create the link list
link *a=(link*)malloc(sizeof(link));
a->elem=i;
a->next=NULL;
temp->next=a;
temp=temp->next;
}
return p;
}
1.3 Display the link list
Now let’s try to display the link list:
void display(link *p){ // Without head node
link* temp=p; // Pointer 'temp' point to the head node
while (temp) {
printf("%d ",temp->elem);
temp=temp->next;
}
printf("\n");
}
However, if the link list has a head node, the code should be sightly changed as follows:
void display1(link *p){ // With head node
link* temp=p;
while (temp->next) {
temp=temp->next;
printf("%d ",temp->elem);
}
printf("\n");
}
Here’s the main function:
int main() {
printf("The initialized link list:\n");
link *p=initLink();
link *q=initLink1();
display(p);
display1(q);
return 0;
}
Output:
We can see that the results are the same.
2 Basic operations of link list
2.1 Insert elements
// p:original link list,elem:new elements,add:inserted position
link * insertElem(link * p,int elem,int add){
link * temp=p; // Create temperary node
for (int i=1; i<add; i++) { // Find the last node before the inserted place
if (temp==NULL) {
printf("Invalid position\n");
return p;
}
temp=temp->next;
}
link * c=(link*)malloc(sizeof(link)); // Create the inserted node 'c'
c->elem=elem;
c->next=temp->next; // Insert the node
temp->next=c;
return p;
}
2.2 Delete elements
//p:original link list, add:the value of the element to be deleted
link * delElem(link * p,int add){
link * temp=p;
for (int i=1; i<add; i++) { // 'temp' points to the last node before the deleted node
temp=temp->next;
}
link * del=temp->next; // Set a pointer point to the deleted node
temp->next=temp->next->next; // Change the pointer
free(del); // Free the node
return p;
}
2.3 Find the element
//p:original link,elem:the value of the target
int selectElem(link * p,int elem){
link * t=p; // Create new pointer 't' and initialized as head pointer 'p'
int i=1;
while (t->next) {
t=t->next;
if (t->elem==elem) {
return i;
}
i++;
}
return -1; // Failed to find the target
}
2.4 Alter the element
//add: position, newElem: the value of the new data field
link *amendElem(link * p,int add,int newElem){
link * temp=p;
temp=temp->next; // Before the traversal, 'temp' points to the first node
for (int i=1; i<add; i++) { // Traverse to the node to be deleted
temp=temp->next;
}
temp->elem=newElem;
return p;
}
2.5 Check the code
int main() {
printf("Initialize the link list:\n");
link *p=initLink();
display(p);
printf("Insert 5 at position 4:\n");
p=insertElem(p, 5, 4);
display(p);
printf("Delete element 3:\n");
p=delElem(p, 3);
display(p);
printf("Find the element 2:\n");
int address=selectElem(p, 2);
if (address==-1) {
printf("Element not found");
}else{
printf("The position of element 2 is:%d\n",address);
}
printf("Change the element at position 3 for 7:\n");
p=amendElem(p, 3, 7);
display(p);
return 0;
}
Output: