연결 목록은 동적 메모리 할당을 사용합니다. 즉, 그에 따라 확장 및 축소됩니다. 노드 모음으로 정의됩니다. 여기서 노드는 데이터와 링크의 두 부분으로 구성됩니다. 데이터, 링크 및 연결 목록의 표현은 다음과 같습니다. -

연결 목록에 대한 작업
C 언어의 연결 목록에는 다음과 같은 세 가지 유형의 작업이 있습니다. -
- 삽입
- 삭제
- 횡단
삭제
아래 주어진 예를 고려하십시오 -
노드 2 삭제

노드 1 삭제

노드 3 삭제

프로그램
다음은 연결 목록의 요소를 삭제하는 C 프로그램입니다. -
#include <stdio.h>
#include <stdlib.h>
struct Node{
int data;
struct Node *next;
};
void push(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 deleteNode(struct Node **head_ref, int position){
//if list is empty
if (*head_ref == NULL)
return;
struct Node* temp = *head_ref;
if (position == 0){
*head_ref = temp->next;
free(temp);
return;
}
for (int i=0; temp!=NULL && i<position-1; i++)
temp = temp->next;
if (temp == NULL || temp->next == NULL)
return;
struct Node *next = temp->next->next;
free(temp->next); // Free memory
temp->next = next;
}
void printList(struct Node *node){
while (node != NULL){
printf(" %d ", node->data);
node = node->next;
}
}
int main(){
struct Node* head = NULL;
push(&head, 7);
push(&head, 1);
push(&head, 3);
push(&head, 2);
push(&head, 8);
puts("Created List: ");
printList(head);
deleteNode(&head, 3);
puts("\n List after Deletion at position 3: ");
printList(head);
return 0;
} 출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
Created List: 8 2 3 1 7 List after Deletion at position 3: 8 2 3 7