我正在尝试插入/删除链表中的第一个元素,但在插入时它不会添加它,在删除时它会启动无限链,我无法识别问题。 我目前正在学习 DSA,所以请忽略代码中添加的任何不必要的注释
我创建的代码,(C语言)
#include <stdio.h>
#include <stdlib.h>
struct Linked_List //structure has to be defined first
{
int number;
struct Linked_List *next;
};
typedef struct Linked_List node;
void create(node *p);
int count(node *p);
void print(node *p);
node *insert(node *head);
node *find(node *p, int key);
node *delete(node *head);
int main() {
node *head;
head = (node *)malloc(sizeof(node));
create(head);
printf("\n");
print(head);
printf("\n\nThe total number of elements in the linked list are :- %d",count(head));
printf("\n");
insert(head);
printf("\n");
print(head);
printf("\n");
delete(head);
printf("\n");
print(head);
}
void create(node *p) {
printf("\nInput number (Input -999 to end) :- ");
scanf("%d", &p->number);
if (p->number == -999) {
p->next = NULL;
} else {
p->next = (node *)malloc(sizeof(node));
create(p->next);
}
}
int count(node *p) {
if (p->next == NULL) { //using the if we are not counting the last element (-999)
return 0; //return 1 will add the last element to total count
} else {
return 1 + count(p->next);
}
//return 1 + count(p->next); //FAIL (as at last returns null pointer pointing to nowhere) returns total elements including -999,
}
void print(node *p) {
// printf("%d --> ", p->number); //this fails as in the end it will pass a null pointer which doesn't points to anywhere
// print(p->next);
if (p->next != NULL) { //this works as it first checks if pointer is null or not
printf("%d --> ", p->number); //if not it prints the number and recursion occurs
if (p->next->next == NULL) { //this if statement is used to print the last value which holds null pointer as outer if will not process it,
printf("%d", p->next->number); //it checks for next value if null pointer then prints its value
}
print(p->next);
}
}
node *insert(node *head) {
node *new; //structure of list
node *preceding; // preceding -> new -> key
int x, key;
printf("\nEnter the new value to be added :- ");
scanf("%d", &x);
printf("\nValue of key item (-999 if last) {-> new -> key}:- ");
scanf("%d", &key);
if (head->number == key) { //case for first position
new = (node *)malloc(sizeof(node));
new->number = x;
new->next = head;
head = new;
} else {
preceding = find(head, key);
if (preceding == NULL) {
printf("\nKey not found!!\n");
} else {
new = (node *)malloc(sizeof(node));
new->number = x;
new->next = preceding->next;
preceding->next = new;
}
}
return head;
}
node *delete(node *head) {
int key; //item to be deleted
node *preceding;
node *p; //temporary node
printf("\nEnter the item to be deleted :- ");
scanf("%d",&key);
if (head->number == key) {
p = head->next;
free(head);
head = p;
} else {
preceding = find(head, key);
if (preceding == NULL) {
printf("\nKey not found!!\n");
} else {
p = preceding->next->next;
free(preceding->next);
preceding->next = p;
}
}
return head;
}
node *find(node *p, int key) {
if (p->next->number == key) { //we know it is not the first so checking 2nd from head
return p;
} else { //checking 3rd from head
if (p->next->next == NULL) {
return NULL;
} else {
find(p->next, key); //recursive call
}
}
}
我尝试获取指针来保存新位置,但由于某种原因,它无法像插入函数那样工作,如下所示
new->next = head;
head = new;
据我了解,您正在返回 head,但没有将其设置在您的 main 方法中。你的意思是做这样的事情:
head = insert(head);
printf("\n");
print(head);
printf("\n");
head = delete(head);
也许您的 find 方法中需要返回:
return find(p->next, key); //recursive call