옹케C 언어 진급, 체인 리스트의 학습 노트(一)
https://www.icourse163.org/learn/ZJU-200001?tid=1207389210#/learn/content?type=detail&id=1212809729&cid=1216239425
1. 체인 시계의 구축
#include  
#include  
#include "node.h"
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct _node{
     
	int value;
	struct _node *next;
}Node; 
Node* add(Node *head,int number); 
int main(int argc, char *argv[])
{
     
    Node *head=NULL;//   
	int number;
	do{
     
		scanf("%d",&number);
		if(number!=-1){
     
			//add to linked-list
			Node *p=(Node*)malloc(sizeof(Node));
			p->value=number;
			p->next=NULL; 
			find the last
			Node *last=head;
			if(last){
     
				while(last->next){
     //   last->next=NULL  ,              
				last=last->next;
				}
			last->next=p;
			}else{
     
				head=p;
			} 
		}
	}while(number!=-1);
	printf("%d",head->value);//                ,           
	return 0;
}
  2. 함수로 봉인
#include  
#include  
/* run this program using the console pauser or add your own getch, system("pause") or input loop */
typedef struct _node{
     
	int value;
	struct _node *next;
}Node; 
Node* add(Node *head,int number); 
int main(int argc, char *argv[])
{
     
	Node *head=NULL;
	int number;
	do{
     
		scanf("%d",&number);
		if(number!=-1){
     
		head=add(head,number);
		}
	}while(number!=-1);
	printf("%d",head->value);
	return 0;
}
Node* add(Node *head,int number)
{
     
	Node *p=(Node*)malloc(sizeof(Node));
		p->value=number;
		p->next=NULL; 
		Node *last=head;
		if(last){
     
			while(last->next){
     
			last=last->next;
			}
		last->next=p;
		}else{
     
			head=p;
		}
		return head;//  :  3、void add(Node **head,int number)                
}
  4. 장점을 다시 개선한다. 우리가 정의한 데이터list를 사용하여 전체 체인 테이블을 대표하고 나중에 수정하기 편리하다.eg: 체인 테이블 저장을 항상 첫 번째부터 검색하지 않고 마지막 번째부터 저장할 수 있도록 데이터를 정의할 수 있다
#include  
#include  
typedef struct _node{
     
	int value;
	struct _node *next;
}Node; 
typedef struct _list{
     
	Node* head;
}List; 
void * add(List *list,int number);
int main(int argc, char *argv[])
{
     
	List list;
	list.head=NULL;
	int number;
	do{
     
		scanf("%d",&number);
		if(number!=-1){
     
	    add(&list,number);
		}
	}while(number!=-1);
	printf("%d",list.head->value);
	return 0;
}
void * add(List *list,int number)
{
     
	Node *p=(Node*)malloc(sizeof(Node));
		p->value=number;
		p->next=NULL; 
		//find the last
		Node *last=list->head;
		if(last){
     
			while(last->next){
     
			last=last->next;
			}
		last->next=p;
		}else{
     
		list->head=p;
		}
}
  5. 4의 예를 실현하는 것이다
#include  
#include  
#include "node.h"
typedef struct _list{
     
	Node* head;
	Node* tail;
}List; 
void * add(List *list,int number);
int main(int argc, char *argv[])
{
     
	List list;
	list.head=NULL;
	list.tail=NULL;
	int number;
	do{
     
		scanf("%d",&number);
		if(number!=-1){
     
	    add(&list,number);
//		head=add(head,number);
		}
	}while(number!=-1);
	printf("%d",list.tail->value);
	return 0;
}
void * add(List *plist,int number)
{
     
	Node *p=(Node*)malloc(sizeof(Node));
		p->value=number;
		p->next=NULL; 
		//find the last
		Node *last=plist->tail;
		if(last){
     
//			while(last->next){
     
//			last=last->next;
//			}
		last->next=p;
		plist->tail=p;
		}else{
     
		plist->head=p;
		plist->tail=p;
		}
}
  체인 시계의 학습(二)
이 내용에 흥미가 있습니까?
현재 기사가 여러분의 문제를 해결하지 못하는 경우 AI 엔진은 머신러닝 분석(스마트 모델이 방금 만들어져 부정확한 경우가 있을 수 있음)을 통해 가장 유사한 기사를 추천합니다:
에세이 - 두 개의 순차적 단일 체인 테이블 결합(반복/비반복)제목: 두 개의 질서정연한 단일 체인 테이블 병합 반복: 비반복:...
텍스트를 자유롭게 공유하거나 복사할 수 있습니다.하지만 이 문서의 URL은 참조 URL로 남겨 두십시오.
CC BY-SA 2.5, CC BY-SA 3.0 및 CC BY-SA 4.0에 따라 라이센스가 부여됩니다.