단일 링크 된 목록과 위치가 주어지면 특정 위치에서 연결된 목록 노드를 삭제하려고합니다. CODE : 나는이 위치 0 위의 노드를 삭제하려고 할 때마다링크 된 목록의 주어진 위치에서 노드 삭제
#include<stdio.h>
#include<stdlib.h>
struct node
{
int data;
struct node* next;
};
void printList(struct node* head_ref)
{
//struct node* head_ref = (struct node*)malloc(sizeof(struct node));
if(head_ref == NULL)
printf("The list is empty");
while(head_ref!=NULL)
{
printf("%d\n",head_ref->data);
head_ref = head_ref->next;
}
}
void insert_beg(struct node **head_ref,int new_data)
{
struct node* new_node = (struct node*)malloc(sizeof(struct node));
new_node->data = new_data;
new_node->next = *head_ref;
*head_ref = new_node;
}
void delete(struct node **head_ref,int position)
{
int i=1;
if(*head_ref == NULL)
return;
struct node *tails,*temp = *head_ref;
if(position == 0)
{
*head_ref = temp->next;
free(temp);
return;
}
while(temp->next!=NULL)
{
tails = temp->next;
temp = temp->next;
if(i == position)
{
tails->next = temp->next;
free(temp);
return;
}
i++;
}
}
int main()
{
struct node *head = NULL;
insert_beg(&head,36);
insert_beg(&head,35);
insert_beg(&head,34);
insert_beg(&head,33);
printList(head);
int position;
printf("Enter the position of the node u wanna delete\n");
scanf("%d",&position);
delete(&head,position);
printf("\n");
printList(head);
}
, 내가 대신 아무것도의 특정 위치에 0을 얻고있다. 내가 어디로 잘못 가고 있는지 알 수 있을까? 예를 들어, 내 목록 입니다 : 33 34 35 36 내 출력 : 33 0 35 36 유효한 출력 (노드 1을 삭제하는 동안) : 앞으로 당신의 삭제 기능 while
루프 tails
및 temp
이동 33 35 36