从后边插入
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
typedef struct node
{
char data;
struct node *nextPtr;
}*LinkList, Lnode;
static void CreateList(LinkList *headPtr);
static void VisitList(LinkList headPtr);
static void DestroyList(LinkList *headPtr);
int main(void)
{
LinkList headPtr = NULL;
CreateList(&headPtr);
VisitList(headPtr);
DestroyList(&headPtr);
getch();
return 0;
}
static void CreateList(LinkList *headPtr)
{
LinkList newPtr, stepPtr;
char c;
if (((*headPtr) = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
(*headPtr) -> nextPtr = NULL;
stepPtr = *headPtr;
while ((c = getchar()) != '\n')
{
if ((newPtr = (LinkList)malloc(sizeof(Lnode))) == NULL)
{
exit(1);
}
newPtr -> data = c;
newPtr -> nextPtr = NULL;
stepPtr -> nextPtr = newPtr;
stepPtr = newPtr;
}
}
static void VisitList(LinkList headPtr)
{
headPtr = headPtr -> nextPtr;
while (headPtr != NULL)
{
putchar(headPtr -> data);
headPtr = headPtr -> nextPtr;
}
putchar('\n');
}
static void DestroyList(LinkList *headPtr)
{
LinkList tempPtr;
while (*headPtr != NULL)
{
tempPtr = *headPtr;
*headPtr = (*headPtr) -> nextPtr;
free(tempPtr);
}
}
日出东方,唯我不败! 做任何东西都是耐得住寂寞,任何一个行业要有十年以上的积累才能成为专家