Programming C: Posts 12 – List of links set in array
Content
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:
#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.
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).
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.
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.
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
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.
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.
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.
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)
#include<stdio.h> #include<stdlib.h> #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 void Init(List *L); //ham khoi tao danh sach rong void Init(List *L); //ham khoi tao danh sach rong int Isempty (List L); //kiem tra danh sach rong int Isfull (List L); //kiem tra danh sach day int Insert_k (List *L, item x, int k); //chen x vao vi tri k void Input (List *L);//nhap danh sach void Output (List L);//xuat danh sach int Search (List L, item x); //tim phan tu x trong danh sach int Del_k (List *L, item *x, int k);//xoa phan tu tai vi tri k int Del_x(List *L, item x);//xoa phan tu x trong danh sach item init_x(); //tao phan tu x. //----------------------------------- 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. } int Isempty (List L) { return (L.size==0); } int Isfull (List L) { return (L.size==N); } item init_x() //khoi tao gia tri x { int temp; scanf("%d",&temp); return temp; } 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; } 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(); } } void Output (List L) { printf("Danh sach: \n"); int i; for (i=0; i<L.size; i++) printf("%5d",L.Elems[i]); printf("\n"); } 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; } 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; } 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; } int main() { List L; Init(&L); Input(&L); Output(L); int lua_chon; printf("Moi ban chon phep toan voi DS LKD:"); printf("\n1: Kiem tra DS rong"); printf("\n2: Do dai DS"); printf("\n3: Chen phan tu x vao vi tri k trong DS"); printf("\n4: Tim mot phan tu trong DS"); printf("\n5: Xoa phan tu tai vi tri k"); printf("\n6: XOa phan tu x trong DS"); printf("\n7: Thoat"); do { printf("\nBan chon: "); scanf("%d",&lua_chon); switch (lua_chon) { case 1: { if (Isempty(L)) printf("DS rong !"); else printf ("DS khong rong !"); break; } case 2: printf ("Do dai DS la: %d.",L.size);break; case 3: { item x; int k; printf ("Nhap vi tri can chen: "); scanf ("%d",&k); if (Insert_k(&L,x,k)) { printf ("DS sau khi chen:\n"); //xuat danh sach sau khi chen Output(L); } break; } case 4: { item x; printf ("Moi ban nhap vao phan tu can tim: "); scanf("%d",&x); int k=Search(L,x); if (k) printf ("Tim thay %d trong DS tai vi tri thu: %d",x,k); else printf ("Khong tim thay %d trong danh sach !",x); break; } case 5: { int k; item x; printf ("Nhap vi tri can xoa: "); scanf ("%d",&k); if (Del_k (&L,&x,k)) { printf ("DS sau khi xoa:\n"); Output(L); } break; } case 6: { item x; printf ("Nhap phan tu can xoa: "); scanf ("%d",&x); if (Del_x(&L,x)) { printf ("DS sau khi xoa:\n"); Output(L); } break; } case 7: break; } }while (lua_chon !=7); return 0; }
[rps-include post=”2703″ shortcodes=”false”]
int lua_chon;
printf(“Please select operations elephant DS LKD:”);
printf(“\n1: Kiem tra DS rong”);
printf(“\n2: Do dai DS”);
printf(“\n3: Chen element k x in place in DS”);
printf(“\n4: Find an element in the DS”);
printf(“\n5: Rub element at position k”);
printf(“\n6: Delete element x in DS”);
printf(“\n7: Exits”);
site n1: \n2: v.v…which means that his star. I see places like print ni normal command. but how they are put to the command switch…his case can pick them so his DC( such as: Kids click 1 it prints out : Kiem tra DS rong . even if you press 2 it prints out : Do dai DS , v.v…. he helped me with explanation
That is just to print out only menu, still time to enter the implementation section below behold switch case…
ct full die rồi a ơi gửi link mới giúp e
Bạn click vào sẽ hiện ra chương trình full nhé.
nhấp nó quay lại trang này ạ. ad giúp e
Bạn xem ở link này nhé: https://ideone.com/rvKBPr
New Kids learn about C and I also do not understand much about tro.A subfolders for e ask that such statements (*The).size or significance L.size syntax is how ạ,why there are dots between 2 sir variable?We wish you answers,Kids thank you sir.
Dots to distinguish expressed access component of 1 variable. Examples sinhvien.ten is to get out there your name sinhvien. You look through all kind of structures should become clearer nhé.
https://www.cachhoc.net/2014/12/19/lap-trinh-c-bai-11-kieu-cau-truc-struct/
E got it a.Hy his expectations are updated post on the website so that people have more learning materials hoi.Em wish him healthy,thank you for answering my question.
Cam on ban.
This error it reported was the account you are executed not root account should not have the right to enforce. You see the account on the machine offline.
syntax: if (Isempty(*The)) ; if (Isfull(*The)) the stars A? e idea is to have the condition if instead, eg
if(Isempty(The) == 0) // to know that this list is empty, hay if (Isfull(The) == N) to know the full list.
Both the while(in) another e also found no condition is >, <, !=,…. how could we understand is that the while loop ends when?
When examining the substantive conditions that we test 1 propositions are true or false. Example 1 == 0 is false, just the same function isEmpty returns true when L is empty, wrong when the stack is not empty.
a more specific is not ?
Then check the conditions nature is right or wrong check, that is true or false, you write a == b or compare 1 some function returns true are false,. In this case the function isEmpty function returns true when it is empty, false if it is not empty. So okay.
typedef int item;
/*kieu item leaf elements
ma cu the o day item la kieu int */
typedef struct
{
item Elems[N]; //array item
int size; //The maximum number of elements to bring
}List; //List type list
may not need to declare: typedef int item;
ma we declared directly into struct always dk no ad
typedef struct
{
int Elems[N];
int size; //The maximum number of elements to bring
}List; //List type list,
and why such lkhai this report:
typedef int item;
/*kieu item leaf elements
ma cu the o day item la kieu int */
typedef struct
{
item Elems[N]; //array item
int size; //The maximum number of elements to bring
}List; //List type list
Well you ah, but himself to type data item when replacing other types easier you ah.
thank ad nhiều
Có thể khai báo trực tiếp. Mình tách riêng ra để sau thay kiểu cho dễ ấy mà.
cho e hỏi muốn viết sang c++ thì chỉ cần thay print và scanf là cout và cin đúng k ạ
Cũng có thể nói alf thế. 😉
Waiter, em dùng VS 2015, khi chạy chương trình hoàn chỉnh thì nó báo lỗi “uninitialized local variable ‘x’ used” in line “if (Insert…” trong case 3 that he, but I declare item x from the first case 3 already, so do you ?
This alone does not clearly. Grilled users vs never. Full code on linux vs dev-c
A explains what if e households (Del_x(&The,x)) if the computer is in understanding how to be ko sir
Del_x function element is deleted in the list values x L. It returns the position it finds x and delete x. If found return means location> 0. If not found it returns 0.
In C / C ++ 0 is wrong, other 0 that right. So Del_x function returns true or false.
If(Del_x()) ie if true.
Ad, e used for visual e asked when the post of ad code in error is the main function x uninitialized, e have created items x; or item * x; then are not sir? Mong ad solution for e sir, Thanks a lot sir f
You assign a value to it is 0 trc not taking view đk
yes, r dc you do sir, Thanks very nhìu sir ad
Welcome. Want to add the edited his element in the array, then how. You just help yourself to
thank you
Star Isfull function(List L) but when the check is Isfull(*The) ?!?! he explained to e is not !?? thank you !!!1
Because L when it is passed to the function 1 pointer (int Insert_k (List *L,…) L should be used to get the value of the cursor. If it is written L address pointer L
A dear to e asked why other functions using function pointers L also search did not sir?
They are learning about pointers should not misread, forward he explained sir.
I thank!
Search function returns only because of what it found, not alter L. When the user wants to change the pointer L.
My teacher asked me when I moved to C ++ code. Why not use cin cout in his ham-defined by sir???? E #include whip sir
using namespace std have more if not write std::cin, std::cout
Oh let me ask Anh Quan
Insert function element at position k if you need to insert a new element just after the last element of the current array, the function can not use his right ?
have too
A dear to e asked. “*” to get the value, “&” to obtain the address. Then list L with L Star The A List, a explanation for e understand with
the declaration, the list L -> L is a pointer variable of type List.
When declaring List L -> is a variable of type List.
God, the passage Del_k(The,&x,in);
i = Search(*The,x);
trouble over a. Stars without deleting straight: Del_k(*The,&x,to) then add Search(The,x), just looking for x and delete it after all