Как вывести адрес первого и последнего элемента списка Си
Помогите пожалуйста. Как адрес первого и последнего элемента односвязного списка. Язык С
#include <stdio.h>
#include <stdlib.h>
struct Tlist
{
int num;
struct Tlist* next;
};
typedef struct Tlist list;
list * pushFront(list* head)
{
list* temp = (list*)malloc(sizeof(list));
scanf("%d", &temp->num);
temp->next = head;
head = temp;
return head;
}
list* pushBack(list* head)
{
list* current = head;
list *temp = (list*)malloc(sizeof(list));
if (head != NULL)
{
while (current->next != NULL)
{
current = current->next;
}
scanf("%d", &temp->num);
current->next = temp;
temp->next = NULL;
return head;
}
else
{
head = temp;
head->next = NULL;
scanf("%d", &temp->num);
return head;
}
}
void printlist(list* head)
{
list* current = head;
if (head == NULL)
{
printf("list is empty");
}
while (current != NULL)
{
printf("%d ", current->num);
current = current->next;
}
}
int main()
{
list* head = NULL;
int choice;
do
{
printf("zapolnennie spiska prodolzhyt? 1-yeas 2-no\n");
scanf("%d", &choice);
switch (choice)
{
case 1:
head = pushBack(head);
break;
case 2:
printlist(head);
break;
}
} while (choice!=2);
return 0;
}
Ответы (1 шт):
Автор решения: AlexGlebe
→ Ссылка
void printaddrfirst(list* head)
{
if (head == NULL)
printf("list is empty");
else
printf("%p ", & head->num);
}
void printaddrlast(list* head)
{
if (head == NULL){
printf("list is empty");
return;}
list * li ;
list * ne ;
ne = head ;
do {
li = ne ;
ne = li -> next ;
} while ( ne ) ;
printf("%p ", & li->num);
}