초보 와 함께 데이터 구 조 를 배 우 는 간단 한 단일 체인 표 실현
토요일 에 도 회사 에 왔 습 니 다. 예전 과 달리 저 는 더 이상 일 을 하지 않 습 니 다. 저 는 배우 고 싶 은 것 을 배 워 야 합 니 다.배 운 데이터 구조 가 많 지 않 아 다 잊 어 버 리 고 정리 하기 시작 했다.
예전 에는 기 존의 c + + 의 STL 로 각종 데이터 구 조 를 실 현 했 지만 이렇게 하면 데이터 구조의 참뜻 만 배 울 수 있 고 배우 지 못 했다.엄 울 민 선생님 의 책 을 다시 들 고 가장 간단 한 링크 부터 시작 하 자.
우선 자신 이 쓴 코드 를 보 세 요.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
 
typedef struct list
{
       int data;
       struct list *next;     
}List;
 
List* list_create(void);
void list_insert(List *head, int value);
void list_delete(List *head, int value);
void list_destroy(List *head);
void list_order(List *head);
void list_print(List *head);
 
int main(void)
{
       List *head;
       int i;
       
       head = list_create();
 
       for(i = 1; i <= 5 ;i++)
              list_insert(head, i);
 
       list_print(head);     
       printf("
");
       
       list_order(head);
       
       list_print(head);     
       printf("
");
       
       list_delete(head, 3);
       
       list_print(head);
       printf("
");
       
       list_destroy(head);
       
       return 0;
}
 
/*
            。
*/
List* list_create(void)
{
       List *head;
       head = (List *)malloc(sizeof(List));
       head->next = NULL;
       
       return head;
}
 
/*
                  value   
*/
void list_insert(List *head, int value)
{
       List *stu, *pos;
       pos = head;     
       
       while(pos->next != NULL) pos = pos->next;
       
       stu = (List *)malloc(sizeof(List));
       stu->data = value;
       stu->next = NULL;
       pos->next = stu;
       pos = pos->next; 
}
 
/*
              value   
*/
 
void list_delete(List *head, int value)
{
       List *pos1, *pos2;
       int flag = 1;
       
       pos1 = head;   
              
       while(pos1->next != NULL)
       {
              if(pos1->next->data == value)
              {
                     pos2 = pos1->next;
                     pos1->next = pos2->next;
                     free(pos2);
                     flag = 0;
                     break;     
              }
              pos1 = pos1->next;
       }
       
       if(flag)
       {
              printf("No emlement
");
       }
       
}
 
/*
                  
*/
void list_print(List *head)
{
       int i = 0;         
       List *pos;
       
       pos = head;
       
       if(pos->next == NULL)
       {
              printf("Empty list
");
              return;
       }
       
       while(pos->next != NULL)
       {
              pos = pos->next;
              printf("%d ", pos->data);      
       }
       printf("
");
}
 
/*
           
*/
void list_destroy(List *head)
{
       List *pos1, *pos2;
       
       pos1 = head;
       
       if(pos1->next == NULL)
       {
              printf("Empty list
");
              return;
       }
       
       while(pos1->next != NULL)
       {
              pos2 = pos1;
              pos1 = pos1->next;
              free(pos2);             
       }
       
       free(pos1);
}
 
/*
             
*/
void list_order(List *head)
{
       List *pos1, *pos;
       List *tail1, *pri1, *mid1;
       List *tail2, *pri2, *mid2;
       
       pos = head;
       
       if(pos->next == NULL)
       {
              printf("Empty list
");
              return;
       }
       
       while(pos->next != NULL)
       {     
              pri1 = pos;
              mid1 = pri1->next;
              tail1 = mid1->next;
 
              pos1 = pos;
              while(pos1->next != NULL)
              { 
                     pri2 = pos1;
                     mid2 = pri2->next;
                     tail2 = mid2->next;              
                            
                    if(mid1->data < mid2->data)
                     {
                            mid1->data ^= mid2->data;
                            mid2->data ^= mid1->data;
                            mid1->data ^= mid2->data;
                     }
                     pos1 = pos1->next; 
              }
              pos = pos->next;
       }
}
 
 
비교적 간단 하면 설명 을 많이 하지 않 겠 다.
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
정수 반전Udemy 에서 공부 한 것을 중얼거린다 Chapter3【Integer Reversal】 (예) 문자열로 숫자를 반전 (toString, split, reverse, join) 인수의 수치 (n)가 0보다 위 또는 ...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.