Programming C: Posts 12 – List of links set in array


List of links can be installed in arrays or pointers. In this article I will show you how to use arrays :), Type this list is often called the next list.

1. Settings (report) list

To declare this list we should have 1 array with a maximum number of elements N data type is item (This is the type of data item overview, when it will be int, structure type float or students). Need more 1 variable size represents the number of elements existing list.
Specifically, the following:

code by nguyenvanquan7826
1
2
3
4
5
6
7
8
9
#define N 100 //so phan tu toi da la 100
typedef int item;
/*kieu cac phan tu la item
ma cu the o day item la kieu int */
typedef struct
{
    item Elems[N]; //mang kieu item
    int size; //so phan tu toi da cua mang
}List; //kieu danh sach List

2. Initialize empty list

Our list is empty when the number of elements in the list by 0. So just declare the size of the one in 0 is.

code by nguyenvanquan7826
1
2
3
4
5
6
void Init(List *L) //ham khoi tao danh sach rong
/*Danh sach L duoc khai bao kieu con tro
de khi ra khoi ham no co the thay doi duoc*/
{
    (*L).size = 0; //size = 0.
}

3. Check the list is empty, complete list

To check the list is empty or full of just the look of the elements of the list by 0 or not (hollow) and by N or not (full).

code by nguyenvanquan7826
1
2
3
4
5
6
7
8
9
int Isempty (List L)
{
    return (L.size==0);
}
 
int Isfull (List L)
{
    return (L.size==N);
}

4. Insert element into position k in list

Before inserting elements in the list we should build 1 function returns data (input data) element to insert it.

code by nguyenvanquan7826
1
2
3
4
5
6
item init_x() //khoi tao gia tri x
{
    int temp;
    scanf("%d",&temp);
    return temp;
}

Then proceed to insert:
During insertion we need to check if the list is full, enter location k to insert and check it, if appropriate (0<to<N =) will proceed to insert.
To insert the element x at position k in list (in the array) we need 1 loop to move from location k elements towards the end of the array, then insert x at position k. Finally we increase the size up 1 unit.

Insert element in the list
Insert element in the list
code by nguyenvanquan7826
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
int Insert_k (List *L, item x, int k)//chen x vao vi tri k
{
    if (Isfull(*L)) //kiem tra danh sach day
    {
        printf("Danh sach day !");
        return 0;
    }
 
    if (k<1 || k>(*L).size+1) //kiem tra dieu kien vi tri chen
    {
        printf("Vi tri chen khong hop le !\n");
        return 0;
    }
    printf ("Nhap thong tin: ");
    x = init_x(); //gan x = ham khoi tao x
    int i;
    //di chuyen cac phan tu ve cuoi danh sach
    for (i = (*L).size; i >= k; i--)
        (*L).Elems[i] = (*L).Elems[i-1];
    (*L).Elems[k-1]=x;//chen x vao vi tri k
    (*L).size++;//tang size len 1 don vi.
    return 1;
}

5. Enter list

Enter as usual array

code by nguyenvanquan7826
01
02
03
04
05
06
07
08
09
10
11
12
void Input (List *L)
{
    int n;
    printf("Nhap so phan tu cua danh sach: ");
    scanf("%d",&(*L).size);
    int i;
    for (i=0; i<(*L).size; i++)
    {
        printf("Nhap phan tu thu %d : ",i+1);
        (*L).Elems[i] = init_x();
    }
}

6. Find element x in list

I Browsing through the end of the list if the value of x is given its location.

code by nguyenvanquan7826
1
2
3
4
5
6
7
8
int Search (List L, item x)
{
    int i;
    for (i=0; i<L.size; i++)
        if (L.Elems[i] == x)
            return i+1;
    return 0;
}

7. Clear kth element in the list

Before deleting one must check list is empty. If you do not empty into position to remove and inspect (appropriate if 0<to<N =).
We use a for loop to run to position k, then put the elements from the previous k 1 1 unit. However, we need to save the value of the element deleted before deleting to withhold information if we need to use it. Finally, the reduced size down 1 unit.

Remove the element from the list
Remove the element from the list
code by nguyenvanquan7826
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
int Del_k (List *L, item *x, int k)
{
    if (Isempty(*L))
    {
        printf("Danh sach rong !");
        return 0;
    }
    if (k<1 || k>(*L).size)
    {
        printf("Vi tri xoa khong hop le !");
        return 0;
    }
    *x=(*L).Elems[k-1]; //luu lai gia tri cua phan tu can xoa
    int i;
    for (i=k-1; i<(*L).size-1; i++) //don cac phan tu ve truoc
    (*L).Elems[i]=(*L).Elems[i+1];
    (*L).size--; //giam size
    return 1;
}

8. Remove element content x in list

To remove the element content x in the list we find elements x conducted before using search function then returns the value of x position, I continue to use the function del_k to delete elements in the position that we find.

code by nguyenvanquan7826
01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
int Del_x (List *L, item x)
{
    if (Isempty(*L))
    {
        printf("Danh sach rong !");
        return 0;
    }
 
    int i = Search(*L,x);
    if (!i)
    {
        printf("Danh sach khong co %d",x);
        return 0;
    }
    do
    {
        Del_k(L,&x,i);
        i = Search(*L,x);
    }
    while (i);
    return 1;
}

9. Complete program (full)


[rps-include post=”2703″ shortcodes=”false”]