I'm asking you a question because the assignment I was doing didn't work out. The structure is a common link list, declaring the head pointer in the main and passing the address value of the head pointer as a parameter to the function. The global variable top is used to determine where the current data is located.
The code currently below will detect only errors when executed.
Structure:
struct ListNode{
int data;
struct ListNode* link;
};
int top = 0;
code:
void DisplayList(ListNode** head){
if(*head == NULL){
printf("List = Empty\n");
}
else{
printf("List = ");
for(;(*head) != NULL; *head = (*head)->link){
printf("%d ",(*head)->data);
}
}
printf("\n");
}
void AddList(ListNode** head){
ListNode* temp = (ListNode*)malloc(sizeof(ListNode));
int num;
printf("Data register) ");
scanf("%d",&num);
temp->data = num;
temp->link = NULL;
top++;
if(*head == NULL){
*head = temp;
}
else{
for(;(*head)->link != NULL; *head = (*head)->link){}
(*head)->link = temp;
}
DisplayList(head);
}
the expected result:
Data register) 10 List = 10
Data register) 20 List = 10 20
Data register) 30 List = 10 20 30