logo

English

이곳의 프로그래밍관련 정보와 소스는 마음대로 활용하셔도 좋습니다. 다만 쓰시기 전에 통보 정도는 해주시는 것이 예의 일것 같습니다. 질문이나 오류 수정은 siseong@gmail.com 으로 주세요. 감사합니다.

Introduce to Singly-linked List

by digipine posted Nov 01, 2017
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print Attachment
?

Shortcut

PrevPrev Article

NextNext Article

Larger Font Smaller Font Up Down Go comment Print Attachment
Singly-linked list is a list of elements in which the elements of the list can be placed anywhere in heap memory. All of list elements are linked together with each other using an explicit link field, that is, by storing the address of the next element in the link field of the previous element. Singly-linked list has a dynamic size and its size can be determined at run-time not compile time. Here the picture which demonstrate a singly-linked list. Their are four elements in the list. The head pointer is the pointer pointing to the first element of the list.
 b184c6674dff9416ccb7aaaa8d2fe018.jpg

 

 
Add a New Element to a Singly-linked List
This picture demonstrates how to add a new elemen to the list. The process is simple as follows:
-  If the existing list is empty we need to insert a new element (or node)  as the starting node
- Otherwise we traverses the existing list to get the pointer to the last node of it;
Create a new node.
Change the next pointer of the last node to the new node.
The next pointer of new node is pointed to NULL and it becomes the last node of the list.
 
c144f0ddfd5789acc378cd0350c95a43.jpg

 

 
 
Implement Singly-linked List in C
 
#include <stdio.h>
#include <stdlib.h>
 
struct node{
    int data;
    struct node *next;
};
 
struct node* add(struct node *head, int data){
    struct node *tmp;
 
    if(head == NULL){
        head=(struct node *)malloc(sizeof(struct node));
        if(head == NULL){
            printf("Error! memory is not available\n");
            exit(0);
        }
        head-> data = data;
        head-> next = head;
    }else{
        tmp = head;
         
        while (tmp-> next != head)
            tmp = tmp-> next;
        tmp-> next = (struct node *)malloc(sizeof(struct node));
        if(tmp -> next == NULL)
        {
            printf("Error! memory is not available\n");
            exit(0);
        }
        tmp = tmp-> next;
        tmp-> data = data;
        tmp-> next = head;
    }
    return head;
}
 
void printlist(struct node *head)
{
    struct node *current;
    current = head;
    if(current!= NULL)
    {
        do
        {
            printf("%d\t",current->data);
            current = current->next;
        } while (current!= head);
        printf("\n");
    }
    else
        printf("The list is empty\n");
 
}
 
void destroy(struct node *head)
{
    struct node *current, *tmp;
     
    current = head->next;
    head->next = NULL;
    while(current != NULL) {
        tmp = current->next;
        free(current);
        current = tmp;
    }
}
 
void main()
{
    struct node *head = NULL;
    head = add(head,1); /* 1 */
    printlist(head);
 
    head = add(head,20);/* 20 */
    printlist(head);
 
    head = add(head,10);/* 1 20 10 */
    printlist(head);
 
    head = add(head,5); /* 1 20 10 5*/
    printlist(head);
     
    destroy(head);
    getchar();
}

 

 

 


List of Articles
No. Subject Author Date Views
23 brute-force 알고리즘을 이용한 패턴 위치 찾기 digipine 2017.10.29 1501
22 C 에서 Overloading 구현 digipine 2017.11.01 1790
21 C++ Atomic 클래스에 대해서 file 엉뚱도마뱀 2017.12.13 2139
20 C++ 컴파일 오류(error): variable 'std::istringstream sstream' has initializer but incomplete type digipine 2017.11.02 21077
19 C++에서 extern의 역할, 기능 digipine 2017.10.29 2656
18 Callback in C++ 와 Delegate 차이점 digipine 2017.11.01 2525
17 C를 이용한 객체지향 프로그래밍 digipine 2017.11.01 568
16 fopen 파일 열기 모드 옵션 정리 digipine 2017.11.02 3894
» Introduce to Singly-linked List file digipine 2017.11.01 1288
14 Linux C 언어로 Shell 명령어 실행하기 digipine 2017.11.01 22587
13 make -j 옵션으로 컴파일 속도 최적화 하기 digipine 2017.11.01 2759
12 MD5 파일 변조 검사 관련 소스 (리눅스/Windows) digipine 2017.10.29 2613
11 Solaris 10에 개발 Tool (gcc,vim,gdb) 설치 digipine 2017.10.29 1257
10 Solaris에서 pmap을 이용하여 백그라운드 프로세스 메모리 크기 구하기 digipine 2017.10.29 28598
9 STL MAP 예제로 공부하기 digipine 2017.10.29 5204
8 Unix C/C++ Input and Output Function Reference digipine 2017.11.01 88072
7 wchar_t에 대하여 digipine 2017.11.01 7343
6 [C/C++] Random UUID String 생성 코드 digipine 2021.10.21 1302
5 [C/C++] 현재시간 구하기 digipine 2017.10.28 2212
4 [Linux] Pthread 사용법, Thread 동기화 총정리 digipine 2017.11.01 294048
Board Pagination Prev 1 2 Next
/ 2