数据结构 1 单链表
#include <stdio.h>
#include <stdlib.h>
#define TURE 1
#define FALSE 0
typedef struct Node{
int data;
struct Node* next;
}Node;
Node* initaList()
{
Node* Head = (Node*)malloc(sizeof(Node));
Head -> data = 0;
Head -> next = NULL;
return Head;
}
void HeadInsert(Node* list, int data)
{
Node* node = (Node*)malloc(sizeof(Node));
node -> next = list -> next;
node -> data = data;
list -> next = node;
list -> data++;
}
void TailInsert(Node* list, int data)
{
Node* tepNode = list;
while(tepNode -> next)
{
tepNode = tepNode -> next;
}
Node* node = (Node*)malloc(sizeof(Node));
node -> next = NULL;
node -> data = data;
tepNode -> next = node;
list -> data++;
}
void DeleteNumber(Node* list, int data)
{
Node* PreNode = list;
Node* node = list -> next;
while(node)
{
if(node -> data == data)
{
PreNode -> next = node -> next;
free(node);
list -> data--;
break;
}
PreNode = node;
node = node -> next;
}
}
void PrintList(Node* list)
{
Node* node = list -> next;
while(node)
{
printf("%d\t",node -> data);
node = node -> next;
}
}
void main(void)
{
printf("--------------------Single link list test------------------\n\n\n");
Node* List = initaList();
for(int i = 0; i < 7; i++)
{
HeadInsert(List, i);
}
for(int j = 1; j < 7; j++)
{
TailInsert(List, j);
}
PrintList(List);
printf("\n\n-------delete 1\n\n");
DeleteNumber(List, 4);
PrintList(List);
printf("\n\n\n------------------------------done--------------------------\n");
}