从具体数值链表中删除节点 [英] Delete node from linked list with specific value
本文介绍了从具体数值链表中删除节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我目前正在写有需要的基础上删除其值的节点功能的程序。
我曾尝试,并试图弄明白。
所有我至今是函数签名:
节点* delete_node(NODE * PTR,整数N,为int * success_flag)
我链表后续的结构:
/ *结构*声明/
typedef结构节点
{
int数据;
结构节点*接下来的;
}节点;
下面有一些code我已经对另一种担忧:
的#include<&stdio.h中GT;
#包括LT&;&stdlib.h中GT;/ *结构的声明* /
typedef结构节点
{
int数据;
结构节点*接下来的;
}节点;/ *函数声明* /
NODE * insert_node(NODE * PTR,NODE *新);
NODE * find_node(NODE * PTR,INT N);
NODE * delete_node(NODE * PTR,整数N,为int * success_flag_ptr);
无效print_backward_iteration(NODE * PTR);
无效print_backward_recursion(NODE * PTR);INT主(INT ARGC,CHAR *的argv [])
{
INT的选择,X,flag_success;
NODE * PTR,*新,*结果; PTR = NULL; 做
{ 的printf(\\ N1 \\ t插入到整数链表\\ n);
的printf(在链表\\ n 2 \\ tFind整数);
的printf(3 \\ tDelete从链表\\ n整数);
的printf(4 \\ t打印出整数落后使用迭代策略\\ n);
输出(5 \\ t打印出整数向后使用递归策略\\ n);
的printf(6 \\ tQuit \\ n);
的printf(\\ n输入1,2,3,4,5,或6:);
scanf函数(%d个,&安培;选择); 开关(选择)
{
情况1: 的printf(\\ n请输入一个整数:);
scanf函数(%d个,&安培; X);
新=(NODE *)malloc的(的sizeof(节点));
新建 - >数据= X;
PTR = insert_node(PTR,新的);
的printf(\\ nNode插入%的D值\\ n,ptr->数据);
打破; 案例2: 的printf(\\ n请输入一个整数:);
scanf函数(%d个,&安培; X);
结果= find_node(PTR,X); 如果(结果== NULL)
{
的printf(\\ n值找不到\\ n);
}
其他
{
的printf(\\ n值%d个被发现\\ n,x)的;
} 打破; 案例3:
的printf(\\ n请输入一个整数:);
scanf函数(%d个,&安培; X);
PTR = delete_node(PTR中,x,&安培; flag_success); 如果(结果== NULL)
{
的printf(\\ n值找不到\\ n);
}
其他
{
的printf(\\ n值%d个已被删除\\ n,x)的;
} 打破; 情况4: print_backward_iteration(PTR);
打破; 情况5: 的printf(\\ n);
print_backward_recursion(PTR);
的printf(\\ n); 打破; 情况6:
的printf(\\ n谢谢您使用此程序\\ n);
打破; 默认:
的printf(\\ n无效的选择,请重试\\ n);
打破; }
}
而(选择= 6!); 的printf(\\ n *** ***程序\\ n结束);
返回0;
}/ *函数insert_node的定义* /
NODE * insert_node(NODE * PTR,NODE *新)
{ 新 - >接下来= PTR;
返回新;}/ *函数find_node的定义* /
NODE * find_node(NODE * PTR,INT N)
{ 而(PTR!= NULL)
{
如果(ptr->数据== N)
{
返回PTR;
}
其他
{
PTR = ptr->接下来,
}
}
返回NULL;
}/ *函数print_backward_iteration的定义* /
无效print_backward_iteration(NODE * PTR)
{
NODE *最后,*电流; 最后= NULL; 的printf(\\ n); 而(PTR!=上)
{
电流= PTR; 而(电流!=上)
{
电流=电流 - >下一个;
} 的printf(%D,电流 - >数据);
最后=电流 - >下一个;
} 的printf(\\ n);}/ *函数print_backward_recursion的定义* /
无效print_backward_recursion(NODE * PTR)
{
NODE *最后,*电流; 最后= NULL; 而(PTR!=上)
{
电流= PTR;
的printf(%D,电流 - >数据);
print_backward_recursion(电流 - >下面);
最后=电流;
}}
解决方案
您必须做这样的事情:
节点* delete_node(NODE * PTR,整数N,为int * success_flag)
{
节点*辅助= NULL; 如果(PTR == NULL)//这意味着孤单无'N'这个链表
{
* success_flag = 0; //表示没有发现价值
返回NULL;
}
如果(ptr-将N == N){//如果这是你的价值要
AUX = ptr->接下来, //辅助将指向剩下的列表
免费(PTR); //释放实际的节点
* success_flag = 1; //标记为成功
返回辅助; //指针返回剩余列表
}
否则//让我们看看剩下的节点
ptr->接着= delete_node(ptr->接着,正,success_flag); 返回PTR;
}
I am currently writing a program that has a function that needs to delete a node based on its value.
I have tried and tried to figure it out.
All I have so far is the function signature:
NODE* delete_node(NODE * ptr, int n, int *success_flag)
My linked list as the follow structure:
/* declaration of structure */
typedef struct node
{
int data;
struct node *next;
} NODE;
Heres some of the code I already have regarding another concerns:
#include <stdio.h>
#include <stdlib.h>
/* declaration of structure */
typedef struct node
{
int data;
struct node *next;
} NODE;
/* declaration of functions */
NODE* insert_node(NODE *ptr, NODE *new);
NODE* find_node(NODE *ptr, int n);
NODE* delete_node(NODE *ptr, int n, int *success_flag_ptr);
void print_backward_iteration(NODE *ptr);
void print_backward_recursion(NODE *ptr);
int main(int argc, char *argv[])
{
int choice, x, flag_success;
NODE *ptr, *new, *result;
ptr = NULL;
do
{
printf("\n1.\tInsert Integer into linked list\n");
printf("2.\tFind integer in linked list\n");
printf("3.\tDelete integer from linked list\n");
printf("4.\tPrint out integers backward using the iterative strategy\n");
printf("5.\tPrint out integers backward using the recursive strategy\n");
printf("6.\tQuit\n");
printf("\nEnter 1,2,3,4,5, or 6: ");
scanf("%d", &choice);
switch(choice)
{
case 1:
printf("\nPlease enter an integer: ");
scanf("%d", &x);
new = (NODE *)malloc(sizeof(NODE));
new->data = x;
ptr = insert_node(ptr, new);
printf("\nNode Inserted with value of %d.\n", ptr->data);
break;
case 2:
printf("\nPlease enter an integer: ");
scanf("%d", &x);
result = find_node(ptr, x);
if (result == NULL)
{
printf("\nValue could not be found.\n");
}
else
{
printf("\nValue %d was found.\n", x);
}
break;
case 3:
printf("\nPlease enter an integer: ");
scanf("%d", &x);
ptr = delete_node(ptr, x, &flag_success);
if (result == NULL)
{
printf("\nValue could not be found.\n");
}
else
{
printf("\nValue %d was deleted.\n", x);
}
break;
case 4:
print_backward_iteration(ptr);
break;
case 5:
printf("\n");
print_backward_recursion(ptr);
printf("\n");
break;
case 6:
printf("\nThank you for using this program.\n");
break;
default:
printf("\nInvalid Choice. Please try again.\n");
break;
}
}
while (choice != 6);
printf("\n*** End of Program ***\n");
return 0;
}
/* definition of function insert_node */
NODE* insert_node(NODE *ptr, NODE *new)
{
new -> next = ptr;
return new;
}
/* definition of function find_node */
NODE* find_node(NODE *ptr, int n)
{
while (ptr != NULL)
{
if (ptr->data == n)
{
return ptr;
}
else
{
ptr = ptr->next;
}
}
return NULL;
}
/* definition of function print_backward_iteration */
void print_backward_iteration(NODE *ptr)
{
NODE *last, *current;
last = NULL;
printf("\n");
while (ptr != last)
{
current = ptr;
while (current != last)
{
current = current -> next;
}
printf("%d ", current -> data);
last = current -> next;
}
printf("\n");
}
/* definition of function print_backward_recursion */
void print_backward_recursion(NODE *ptr)
{
NODE *last, *current;
last = NULL;
while (ptr != last)
{
current = ptr;
printf("%d ", current -> data);
print_backward_recursion(current -> next);
last = current;
}
}
解决方案
You have to do something like this:
NODE* delete_node(NODE * ptr, int n, int *success_flag)
{
Node *aux = NULL;
if(ptr == NULL) // this means that theres is no 'n' in this linked list
{
*success_flag = 0; // means no value found
return NULL;
}
if(ptr-> n == n){ // if this is the value you want
aux = ptr->next; // aux will point to the remaining list
free(ptr); // free the actual node
*success_flag = 1; // mark as success
return aux; // return the pointer to the remaining list
}
else // lets see the remaining nodes
ptr->next = delete_node(ptr->next,n,success_flag);
return ptr;
}
这篇关于从具体数值链表中删除节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文