#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}Node;
Node *initList()
{
Node *pHead = (Node *)malloc(sizeof(Node));
if (pHead== NULL)
return NULL;
else
return pHead;
}
void insertList(Node *list, int data)
{
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL)
return;
node->data = data;
node->next = list->next;
list->next = node;
}
Node * findNode(Node *list, int data)
{
Node *p = list->next;
while (p)
{
if (p->data == data)
break;
p = p->next;
}
return p;
}
void deleteNode(Node *list, int data)
{
Node *p = list;
Node *pData = NULL;
while (p->next)
{
if (p->next->data == data)
{
pData = p->next;
p->next = pData->next;
free(pData);
break;
}
p = p->next;
}
}
void printList(Node *list )
{
printf("-------------\n");
Node *p = list->next;
while (p)
{
printf("%d\n", p->data);
p = p->next;
}
return;
}
int main()
{
Node *list = initList();
if (list == NULL)
return 0;
insertList(list,1);
insertList(list,2);
insertList(list,3);
insertList(list,4);
printList(list);
deleteNode(list,1);
printList(list);
return 0;
}
#include <stdlib.h>
typedef struct node
{
int data;
struct node *next;
}Node;
Node *initList()
{
Node *pHead = (Node *)malloc(sizeof(Node));
if (pHead== NULL)
return NULL;
else
return pHead;
}
void insertList(Node *list, int data)
{
Node *node = (Node *)malloc(sizeof(Node));
if (node == NULL)
return;
node->data = data;
node->next = list->next;
list->next = node;
}
Node * findNode(Node *list, int data)
{
Node *p = list->next;
while (p)
{
if (p->data == data)
break;
p = p->next;
}
return p;
}
void deleteNode(Node *list, int data)
{
Node *p = list;
Node *pData = NULL;
while (p->next)
{
if (p->next->data == data)
{
pData = p->next;
p->next = pData->next;
free(pData);
break;
}
p = p->next;
}
}
void printList(Node *list )
{
printf("-------------\n");
Node *p = list->next;
while (p)
{
printf("%d\n", p->data);
p = p->next;
}
return;
}
int main()
{
Node *list = initList();
if (list == NULL)
return 0;
insertList(list,1);
insertList(list,2);
insertList(list,3);
insertList(list,4);
printList(list);
deleteNode(list,1);
printList(list);
return 0;
}