#include <stdio.h>
#include <stdlib.h>
struct node{
int value;
struct node* pnext;
};
struct node* createlinklist(int length);
void linklisttraverse(struct node* phead);
void deletelastone(struct node** phead);
int main(int argc, char** argv)
{
struct node* phead = createlinklist(atoi(argv[1]));
linklisttraverse(phead);
deletelastone(&phead);
linklisttraverse(phead);
return 0;
}
struct node* createlinklist(int length)
{
int i = 0;
int val;
struct node* phead = NULL;
for(i = 0; i < length; i++)
{
struct node* pnew = (struct node*)malloc(sizeof(struct node));
scanf("%d", &val);
pnew->value = val;
pnew->pnext = phead;
phead = pnew;
}
return phead;
}
void deletelastone(struct node** phead)
{
if(NULL == (*phead)->pnext)
{
free(*phead);
*phead = NULL;
return;
}
struct node * temp = *phead;
while(temp->pnext->pnext)
{
temp = temp->pnext;
}
struct node* last = temp->pnext;
free(last);
temp->pnext = NULL;
}
void linklisttraverse(struct node* phead)
{
while(phead)
{
printf("%d | ", phead->value);
phead = phead->pnext;
}
printf("\n");
}
算法1-3-19答案
最新推荐文章于 2023-07-04 11:10:08 发布