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

연결 목록의 유형
연결 목록에는 다음과 같은 네 가지 유형이 있습니다. -
- 단일/단일 연결 목록
- 이중/이중 연결 목록
- 순환 단일 연결 목록
- 원형 이중 연결 목록
재귀 방법을 사용하여 연결 목록의 길이를 찾는 데 사용한 논리는 -
입니다.int length(node *temp){
if(temp==NULL)
return l;
else{
l=l+1;
length(temp->next);
}
} 프로그램
다음은 연결 리스트의 길이를 구하는 C 프로그램입니다 -
#include <stdio.h>
#include <stdlib.h>
typedef struct linklist{
int data;
struct linklist *next;
}node;
int l=0;
int main(){
node *head=NULL,*temp,*temp1;
int len,choice,count=0,key;
do{
temp=(node *)malloc(sizeof(node));
if(temp!=NULL){
printf("\nenter the elements in a list : ");
scanf("%d",&temp->data);
temp->next=NULL;
if(head==NULL){
head=temp;
}else{
temp1=head;
while(temp1->next!=NULL){
temp1=temp1->next;
}
temp1->next=temp;
}
}else{
printf("\nMemory is full");
}
printf("\npress 1 to enter data into list: ");
scanf("%d",&choice);
}while(choice==1);
len=length(head);
printf("The list has %d no of nodes",l);
return 0;
}
//recursive function to find length
int length(node *temp){
if(temp==NULL)
return l;
else{
l=l+1;
length(temp->next);
}
} 출력
위의 프로그램을 실행하면 다음과 같은 결과가 나온다 -
Run 1: enter the elements in a list: 3 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 1 enter the elements in a list: 56 press 1 to enter data into list: 0 The list has 3 no of nodes Run 2: enter the elements in a list: 12 press 1 to enter data into list: 1 enter the elements in a list: 45 press 1 to enter data into list: 0 The list has 2 no of nodes