#include<stdio.h>
#include<stdlib.h>
struct node {
int num;
struct node* nextptr;
};
void createlisthead(struct node** headptr)
{
*headptr=(struct node*)malloc(sizeof(struct node));
if (*headptr != NULL)
{
(*headptr)->nextptr = NULL;
}
}
void insert(struct node* headptr, int number)
{
struct node* newptr = NULL;
struct node* previousptr = NULL;
struct node* currentptr = NULL;
newptr = (struct node*)malloc(sizeof(struct node));
if (newptr != NULL)
{
newptr->num = number;
newptr->nextptr = NULL;
previousptr = headptr;
currentptr = previousptr->nextptr;
while (currentptr != NULL)
{
previousptr = currentptr;
currentptr = currentptr->nextptr;
}
previousptr->nextptr = newptr;
}
}
void printlist(struct node* headptr)
{
struct node* currentptr = headptr->nextptr;
while (currentptr != NULL)
{
printf_s("%d ", currentptr->num);
currentptr = currentptr->nextptr;
}
}
int main()
{
struct node* headptr;
createlisthead(&headptr);
int n;
scanf_s("%d", &n);
while (n != -1)
{
insert(headptr, n);
scanf_s("%d", &n);
}
printlist(headptr);
return 0;
}
C语言链表之空头链表的创立,及元素的插入
最新推荐文章于 2023-06-14 20:41:55 发布