单向链表常用函数实现
#include<stdio.h>
#include<stdlib.h>
#include<stdbool.h>
#define Elemtype int
struct node{
Elemtype val;
struct node* next;
};
struct node* Create_List();
struct node* Create_Node(Elemtype val);
struct node* Clear_List(struct node* head);
void Destory_List(struct node** head);
bool List_IsEmpty(struct node* head);
int List_Length(struct node* head);
bool Get_List_Element(struct node* head,int i,Elemtype* e);
void Print_List(struct node* head);
bool List_Insert(struct node* head, int i,struct node* new_node);
void List_Append(struct node*head,struct node*new_node);
void List_Insert_in_First(struct node *head,struct node* new_node);
bool delete_List(struct node* head,int i);
struct node* Create_List()
{
int elemt_num;
printf("Enter the number of elemt: ");
scanf("%d",&elemt_num);
struct node*head = (struct node*)malloc(sizeof(struct node));
struct node* temp = head;
head->next = NULL;
for(int i = 0; i < elemt_num; i++){
Elemtype elemt;
printf("Node #%d val:",i+1);
scanf("%d", &elemt);
struct node* p = Create_Node(elemt);
temp->next = p;
p->next = NULL;
temp = temp->next;
}
return head;
}
struct node* Create_Node(Elemtype val)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->next = NULL;
temp->val = val;
return temp;
}
struct node* Clear_List(struct node* head)
{
struct node *temp = NULL;
while(head->next != NULL){
temp = head->next;
head->next = temp->next;
free(temp);
temp = NULL;
}
return head;
}
void Destory_List(struct node** head)
{
Clear_List(*head);
free(*head);
*head = NULL;
return;
}
bool List_IsEmpty(struct node* head)
{
if(head->next == NULL)
return true;
return false;
}
int List_Length(struct node* head)
{
struct node* temp = head->next;
int num = 0;
while(temp){
++num;
temp = temp->next;
}
return num;
}
bool Get_List_Element(struct node* head,int i,Elemtype *e)
{
struct node* temp = head->next;
if(List_Length(head) >= i && i > 0){
for(int j = 1;j <= i;j++)
temp = temp->next;
*e = temp->val;
return true;
}
else
printf("The number of i is invalid,may be more than length of Linked List.\n");
return false;
}
void Print_List(struct node* head)
{
struct node*temp = head->next;
int i = 1;
while(temp){
printf("Node#%d val: %d\n",i,temp->val);
i++;
temp = temp->next;
}
return;
}
bool List_Insert(struct node* head, int i,struct node* new_node)
{
struct node* temp = head->next;
if(List_Length(head) >= i && i > 0){
int j = 1;
while(j != i-1){
temp = temp->next;
j++;
}
new_node->next = temp->next;
temp->next = new_node;
return true;
}
else
printf("The number of i is invalid,may be more than length of Linked List.\n");
return false;
}
void List_Append(struct node*head,struct node* new_node)
{
struct node* temp = head;
while(temp->next)
temp = temp->next;
temp->next = new_node;
new_node->next = NULL;
return;
}
void List_Insert_in_First(struct node *head,struct node* new_node)
{
new_node->next = head->next;
head->next = new_node;
return;
}
bool delete_List(struct node* head,int i)
{
struct node* temp = head->next,*prev_temp = head;
if(List_Length(head) >= i && i > 0){
int j = 1;
while(j != i){
temp = temp->next;
prev_temp = prev_temp->next;
j++;
}
prev_temp->next = temp->next;
free(temp);
temp = NULL;
return true;
}
else
return false;
}