C language how to establish a linked list and implement the addition deletion search and modification details
- 2020-06-19 11:11:11
- OfStack
preface
The following is an C language linked list and I completed the operation of adding, deleting, checking and changing the procedure. For the convenience of learning, I divided the whole procedure into two parts: header file and main function:
1. Header file (Function part)
(1) Initialize the function
#include <stdio.h>
#include <stdlib.h>
typedef struct {
int *head;
int length;
int capacity;
} Toslist; //Toslist type
// Initialize the sequence table
Toslist initSeqlist() {
Toslist list;
list.length = 0;
list.capacity = 5;
list.head = (int *)malloc(10 * sizeof(int));
if (!list.head)
{
printf(" Initialization failed! \n");
exit(0);
}
return list;
}
(2) Print function
// Print sequence table
void displayList(Toslist list) {
for (int i = 0; i < list.length; i++) {
printf("%d ", list.head[i]);
}
printf("\n");
}
(3) Insert function
// Insert elements
Toslist add(Toslist list, int elem, int pos) {
if (list.length == list.capacity) {
int *temp = (int *)realloc(list.head, (list.capacity + 1) * sizeof(int));// Determine whether the space is enough, not enough on the other linked list
// Don't use directly head The introduction of temp Prevents space allocation failure head Losing the original list
if (!temp) {
list.head = temp;
list.capacity += 1;
}
}
// The insertion position and subsequent elements are moved back
for (int i = list.length - 1; i >= pos; i--) {
list.head[i + 1] = list.head[i];
}
list.head[pos] = elem;
list.length ++;
return list;
if (pos > list.length || pos < 0)
printf(" Error inserting position! \n");
return list;
}
(4) Delete the function
// Remove elements
Toslist delete(Toslist list, int pos) {
for (int i = pos; i < list.length - 1; i++) {
list.head[i] = list.head[i + 1];
}
list.length--;
return list;
if (pos < 0 || pos > list.length) {
printf(" Incorrect delete position! \n");
return list;
}
}
(5) Find the function
// check
int search(Toslist list, int elem) { //elem Is the element you are looking for
// In order to find
for (int i = 0; i < list.length; i++) {
if (elem == list.head[i]) {
return i;
}
}
return 0;
}
(6) Replace the function
// change
Toslist modify(Toslist list, int elem, int val) { //val It's the element that's going to be replaced
int pos = search(list, elem); // Gets the location of the element to be replaced
list.head[pos] = val;
return list;
}
2. The main function
int main() {
Toslist list = initSeqlist();
int Addpos = -1, Addnum, Delpos, Serachnum,Modifynum;
printf(" Please enter the 5 Integer element \n");
for (int i = 0; i < 5; i++) {
scanf("%d", &list.head[i]);
list.length++;
}
printf(" The elements in the sequence table are: \n");
displayList(list);
// Insert elements
printf(" After which element do I insert the element? \n");
while (Addpos < 0 || Addpos > list.length) {
scanf("%d", &Addpos);
if (Addpos < 0 || Addpos > list.length)
printf(" Please enter the correct location! \n");
};
printf(" Please enter the elements you want to insert: \n"); scanf("%d", &Addnum);
printf(" At the end of the sequence table %d Insert element after element %d get \n", Addpos, Addnum);
list = add(list, Addnum, Addpos);
displayList(list);
// Remove elements
printf(" Which element in the subscript of the order table do you want to delete? \n"); scanf("%d", &Delpos);
printf(" After deletion: \n");
list = delete(list, Delpos);
displayList(list);
// To find the
printf(" Enter the element you want to look for \n"); scanf("%d", &Serachnum);
int pos = search(list, Serachnum);
if(pos)
printf(" The element %d Is the position of %d a \n", Serachnum, pos+1);
if(!pos){
printf(" The element is not in the table \n");
}
// Modify the
printf(" Please enter the elements that need to be modified: \n");scanf("%d",&Serachnum);
printf(" Please enter the number to replace: \n");scanf("%d",&Modifynum);
printf(" will %d Modified to %d Get: \n", Serachnum, Modifynum);
list = modify(list, Serachnum, Modifynum);
displayList(list);
free(list.head);
list.head = NULL;
return 0;
}
The above procedure I have finished debugging, if the procedure has multifarious place, welcome to criticize and correct!
conclusion