Linked List Management (C language) 1. Insert an integer to the linked list if it does not
Fantastic news! We've Found the answer you've been seeking!
Question:
Linked List Management (C language)
1. Insert an integer to the linked list if it does not yet occur in the list. If already exist, do nothing. (Tips: before insertion, check whether the value to be inserted already exist)2. Delete an integer from the head of the linked list3. Delete an integer from the end of the linked list4. Delete an integer from the middle elemenmt of the linked list5. Delete all matching integers in a range (inclusive) from the linked listSample input102 4 6 5 3 8 6 7 11 93 4Sample output:5 7 8 9using the following code
#include #include #include typedef struct linked_list_node{ int value; struct linked_list_node* next;} Node;typedef Node* List;int Exist(List p, int value){ // judge whether linked list p contains the value // you should visit each node of the linked list one by one, and check whether it is equal to the value // if you find it then return 1, otherwise return 0 while(p!=NULL) { if(p->value==value) return 1; p = p->next; } return 0;}void Print(List p){ // print all integers from the head of linked list in one line(separated by a space), in other words, you should firstly print the integer p points to // to be more specific, print p->value and then move p to the next node p->next recursively, until p points to NULL while(p!=NULL) { printf("%d ", p->value); p = p->next; } printf(" ");}int Num(List p){ int num = 0; while(p!=NULL) { num = num + 1; p = p->next; } return num;}int MiddleIndex(List p){ }List Delete_a(List p){ }List Delete_b(List p){ }List Delete_c(List p){ }List Delete_d(List p, int value_min, int value_max){ }List Insert(List p, int value){ }int main(){ List p = NULL; int n, m; int value, value_min, value_max; // printf("List initialization: "); // printf("How many integers in list initially: "); scanf("%d", &n); // printf("What are the integers: "); for(int i=0; i;> { int value; scanf("%d", &value); p = Insert(p, value); } // printf("Delete a value from head "); p = Delete_a(p); // printf("Delete a value from end "); p = Delete_b(p); // printf("Delete a value from first middle "); p = Delete_c(p); scanf("%d%d", &value_min, &value_max); p = Delete_d(p, value_min, value_max); // printf("Current array: "); Print(p); return 0;}
Posted Date: