Mình đang học đến phần danh sách liên kết đơn, thì thấy cái ký hiệu -> liệu nó có tương ứng với dấu . ko? kiểu như lấy 1 thuộc tính của một đối tượng đó ?
cho mình hỏi thêm làm thế nào để in ra các phần tử trong danh sách liên kết đơn vậy ?
#include <stdio.h>
struct node {
int item;
struct node *next;
};
typedef struct node *listnode;
void Insert_Begin(listnode *p, int x){
listnode q;
q = (listnode)malloc(sizeof(struct node));
q->item=x;
q->next=*p;
*p=q;
}
void Insert_End(listnode *p, int x){
listnode q,r;
q = (listnode)malloc(sizeof(struct node));
q->item = x;
q->next = NULL;
if(*p==NULL) *p=q;
else{
r = *p;
while (r->next != NULL) {
r=r->next;
}
r->next=q;
}
}
void Insert_Middle(listnode *p,int position, int x){
int count = 1, found=0;
listnode q,r;
r=*p;
while((r!=NULL)&& (found==0)){
if(count == position){
q=(listnode)malloc(sizeof(struct node));
q->item=x;
q->next=r->next;
r->next=q;
found=1;
}
count++;
r=r->next;
}
if(found==0)
printf("Khong tim thay vi tri can chen.");
}
void Remove_Begin(listnode *p){
listnode q;
if(*p==NULL) return;
q=*p;
*p=(*p)->next;
q->next=NULL;
free(q);
}
void Remove_End(listnode *p){
listnode q,r;
if(*p==NULL) return;
if((*p)->next == NULL){
Remove_Begin(*p);
return;
}
r=*p;
while(r->next==NULL){
q=r;
r=r->next;
}
q->next=NULL;
free(r);
}
void Remove_Middle(listnode *p,int position){
int count =1,found=0;
listnode q,r;
r=*p;
while ((r !=NULL)&&(found==0)){
if(count == position){
q = r->next;
r->next=q->next;
q->next=NULL;
free(q);
found=1;
}
count++;
r=r->next;
}
if(found==0){
printf("Khong tim thay vi tri can xoa.");
}
}
int main(){
printf("Tao ra mot danh sach:");
listnode p;
p=NULL;
Insert_Begin(&p,4);
listnode r;
r=p;
while(r->next != NULL){
printf("%d",*(r.item));
r=r->next;
}
}