> 백엔드 개발 > C++ > 본문

C 언어에서는 주어진 인덱스에 연결된 목록 노드를 인쇄합니다.

WBOY
풀어 주다: 2023-08-26 21:21:04
앞으로
1088명이 탐색했습니다.

주어진 인덱스에 연결된 목록의 노드 데이터를 인쇄해야 합니다. 배열 연결 목록과 달리 일반적으로 인덱스가 없으므로 전체 연결 목록을 순회하여 특정 항목에 도달하면 데이터를 인쇄해야 합니다.

목록에 노드 29, 34, 43, 56 및 88이 포함되어 있고 값 인덱스의 수는 1, 2, 4이고 출력은 34, 43, 88인 인덱스의 노드가 됩니다.

C 언어에서는 주어진 인덱스에 연결된 목록 노드를 인쇄합니다.

Example

Linked list: 29->34->43->56->88
Input: 1 2 4
Output: 34 43 88
로그인 후 복사

위의 연결 목록 표현에서 노란색으로 강조 표시된 노드는 노드입니다. 인쇄되거나 특정 인덱스에 있는 노드입니다.

여기서 사용되는 접근 방식은 노드가 탐색될 때마다 증가하는 1로 초기화된 하나의 포인터와 하나의 카운터 변수를 사용하는 것입니다. 카운터는 키 값과 일치합니다. 키가 카운터 값과 일치하면 노드 구조를 가리키는 포인터는 노드의 데이터를 인쇄하고 다음 노드로 증가하여 특정 키의 노드를 제공합니다.

아래 코드는 주어진 알고리즘의 c 구현을 보여줍니다.

Algorithm

START
   Step 1 -> create node variable of type structure
      Declare int data
      Declare pointer of type node using *next
   Step 2 -> create struct node* intoList(int data)
      Create newnode using malloc
      Set newnode->data = data
      newnode->next = NULL
      return newnode
   step 3 -> Declare function void displayList(struct node *catchead)
      create struct node *temp
      IF catchead = NULL
         Print list is empty
         return
      End
      Set temp = catchead
      Loop While (temp != NULL)
         print temp->data
         set temp = temp->next
      End
   Step 4 -> Declare Function int search(int key,struct node *head)
      Set int index
      Create struct node *newnode
      Set index = 0 and newnode = head
      Loop While (newnode != NULL & newnode->data != key)
         Set index++
         Set newnode = newnode->next
      End
      return (newnode != NULL) ? index : -1
   step 5 -> In Main()
      create node using struct node* head = intoList(9)
      call displayList(head)
      set index = search(24,head)
      IF (index >= 0)
         Print index
      Else
         Print not found in the list
      EndIF
STOP
로그인 후 복사

Example

#include <stdio.h>
#include <stdlib.h>
//structure of a node
struct node {
   int data;
   struct node *next;
};
struct node* intoList(int data) {
   struct node* newnode = (struct node*)malloc(sizeof(struct node));
   newnode->data = data;
   newnode->next = NULL;
   return newnode;
}
//funtion to display list
void displayList(struct node *catchead) {
   struct node *temp;
   if (catchead == NULL) {
      printf("List is empty.</p><p>");
      return;
   }
   printf("elements of list are : ");
   temp = catchead;
   while (temp != NULL) {
      printf("%d ", temp->data);
      temp = temp->next;
   }
   printf("</p><p>");
}
//function to search element
int search(int key,struct node *head) {
   int index;
   struct node *newnode;
   index = 0;
   newnode = head;
   while (newnode != NULL && newnode->data != key) {
      index++;
      newnode = newnode->next;
   }
   return (newnode != NULL) ? index : -1;
}
int main() {
   int index;
   struct node* head = intoList(9); //inserting elements into a list
   head->next = intoList(76);
   head->next->next = intoList(13);
   head->next->next->next = intoList(24);
   head->next->next->next->next = intoList(55);
   head->next->next->next->next->next = intoList(109);
   displayList(head);
   index = search(24,head);
   if (index >= 0)
      printf("%d found at position %d</p><p>", 24, index);
   else
      printf("%d not found in the list.</p><p>", 24);
   index=search(55,head);
   if (index >= 0)
      printf("%d found at position %d</p><p>", 55, index);
   else
   printf("%d not found in the list.</p><p>", 55);
}
로그인 후 복사

输ude

如果我们运行上面的程序,它将生成以下输ude。

elements of list are : 9 76 13 24 55 109
24 found at position 3
55 found at position 4
로그인 후 복사

위 내용은 C 언어에서는 주어진 인덱스에 연결된 목록 노드를 인쇄합니다.의 상세 내용입니다. 자세한 내용은 PHP 중국어 웹사이트의 기타 관련 기사를 참조하세요!

원천:tutorialspoint.com
본 웹사이트의 성명
본 글의 내용은 네티즌들의 자발적인 기여로 작성되었으며, 저작권은 원저작자에게 있습니다. 본 사이트는 이에 상응하는 법적 책임을 지지 않습니다. 표절이나 침해가 의심되는 콘텐츠를 발견한 경우 admin@php.cn으로 문의하세요.
최신 이슈
인기 튜토리얼
더>
최신 다운로드
더>
웹 효과
웹사이트 소스 코드
웹사이트 자료
프론트엔드 템플릿
회사 소개 부인 성명 Sitemap
PHP 중국어 웹사이트:공공복지 온라인 PHP 교육,PHP 학습자의 빠른 성장을 도와주세요!