C 언어 로 링크 를 작성 합 니 다.

본 논문 의 사례 는 C 언어 가 링크 를 작성 하 는 구체 적 인 코드 를 공유 하여 여러분 께 참고 하 실 수 있 습 니 다.구체 적 인 내용 은 다음 과 같 습 니 다.
체인 테이블

갖 춘 기본 기능:
1.헤드 링크 만 들 기

struct Node* Creatlist(){//     
 struct Node *headnode = (struct Node*)malloc(sizeof(struct Node));//        ,    
 headnode->next = NULL;//     
 return headnode;
}
2.노드 만 들 기

struct Node* Creatnode(int num){//    ,  ,     
 struct Node* newnode = (struct Node*)malloc(sizeof(struct Node));//        ,    
 newnode->num = num;
 newnode->next = NULL;//     
 return newnode;
}
3.노드 삽입

void Insetlist(struct Node* list, int num){//   
 struct Node* insetnode = Creatnode(num);
 if (list != NULL){
  insetnode->next = list->next;
  list->next = insetnode;
 }
 else{
  printf("     
"); } } void Insetlists(struct Node* headnode, int n, int num){// n , , n , num int i = 1; struct Node *t = headnode; while (i < n&& t!= NULL){ t = t->next; i++; } if (t != NULL){ struct Node* insetnode = Creatnode(num); insetnode->next = t->next; t->next = insetnode; } else{ printf("
"); } }
4.노드 수정

void Modifynode(struct Node* headnode, int n){//    ,    ,    n   
 struct Node* list = headnode;
 int i = 0;
 while (i < n&&list != NULL){
  list = list->next;
  i++;
 }
 if (list != NULL){
  printf("         
"); int j = 0; scanf("%d", &j); list->num = j; } else{ printf("
"); } }
5.노드 삭제

두 개의 지침 을 정의 합 니 다.하 나 는 노드 를 삭제 하 는 이전 노드 를 가리 키 고 하 나 는 삭제 할 노드 를 가리 키 는 것 입 니 다.

void Deletnode(struct Node* headnode, int n){//   n   ,
 int i = 1;
 struct Node *strat = headnode;
 struct Node *end = headnode->next;

 while (i < n&&end != NULL){
  strat = strat->next;
  end = end->next;
  i++;
 }
 if (end != NULL){
  strat->next = end->next;
  free(end);

 }
 else{
  printf("     
"); } }
6.인쇄 노드

void Printnode(struct Node* headnode){//    
 struct Node* list = headnode;
 while ((list->next) != NULL){
  list = list->next;
  printf("%d\t",  list->num);
 }
 printf("
"); }
7.주 함수

int main(){
 struct Node* list = Creatlist();

 Insetlists(list, 1, 1);
 Printnode(list);
 int i = 0;
 printf("         
"); scanf("%d", &i); Modifynode(list, i); Printnode(list); printf("
"); int n = 0; scanf("%d", &n); Deletnode(list, n); Printnode(list); system("pause"); return 0; }
이상 이 바로 본 고의 모든 내용 입 니 다.여러분 의 학습 에 도움 이 되 고 저 희 를 많이 응원 해 주 셨 으 면 좋 겠 습 니 다.

좋은 웹페이지 즐겨찾기