#include<stdio.h>
#include<stdlib.h>
struct Node{
int data;
struct Node *next;
};
struct Node* createlist()
{
struct Node* headNode = (struct Node*)malloc(sizeof(struct Node));
headNode->next=NULL;
return headNode;
}
struct Node* createNode(int data)
{
struct Node* newNode = (struct Node*)malloc(sizeof(struct Node));
newNode->data=data;
newNode->next=NULL;
return newNode;
}
void printlist(struct Node* headNode)
{
struct Node* pMove = headNode->next;
while (pMove)
{
printf("%d\t",pMove->data);
pMove = pMove->next;
}
printf("\n");
}
void insertNodeByHead(struct Node* headNode,int data)
{
struct Node* newNode = createNode(data);
newNode->next = headNode->next;
headNode->next = newNode;
}
void deleteNodeByAppoin(struct Node* headNode,int posData)
{
struct Node* posNode = headNode->next;
struct Node* posNodeFront = headNode;
if(posNode == NULL)
{
printf("无法删除,链表为空\n");
}
else
{
while(posNode->data != posData)
{
posNodeFront = posNode;
posNode = posNodeFront->next;
if(posNode == NULL){
printf("无法删除,链表为空\n");
return ;
}
}
posNodeFront->next = posNode->next;
free(posNode);
}
}
int main(){
struct Node* list = createlist();
insertNodeByHead(list,1);
insertNodeByHead(list,2);
insertNodeByHead(list,3);
printlist(list);
deleteNodeByAppoin(list,2);
printlist(list);
system("pause");
return 0;
}