#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct MyNode{
int num;
struct MyNode *next;
}STNode_def;
#define NODE_SIZE sizeof(STNode_def)
#define SUCCESS 0
#define FAILURE -1
STNode_def* list_init()
{
STNode_def* head_ptr = (STNode_def*)malloc(NODE_SIZE);
if (head_ptr == NULL)
{
perror("malloc");
return NULL;
}
memset(head_ptr, 0x00, NODE_SIZE);
head_ptr->next = NULL;
return head_ptr;
}
void list_print(STNode_def* head)
{
STNode_def* p = head->next;
while (p != NULL)
{
printf("%d ", p->num);
p = p ->next;
}
printf("\n");
}
int list_size(STNode_def* head)
{
STNode_def* p = head;
int size = 0;
while (p->next != NULL)
{
p = p->next;
size++;
}
return size;
}
int list_push_back(STNode_def* head, int value)
{
STNode_def* p = head;
STNode_def* pnew_node = (STNode_def*)malloc(NODE_SIZE);
if (pnew_node == NULL)
{
printf("param is null\n");
return FAILURE;
}
memset(pnew_node, 0x00, sizeof(NODE_SIZE));
pnew_node->num = value;
while (p->next != NULL)
{
p = p->next;
}
p->next = pnew_node;
pnew_node->next = NULL;
return SUCCESS;
}
int list_push_front(STNode_def* head, int value)
{
STNode_def* pnew_node = (STNode_def*)malloc(NODE_SIZE);
if (pnew_node == NULL)
{
printf("param is null\n");
return FAILURE;
}
memset(pnew_node, 0x00, sizeof(NODE_SIZE));
pnew_node->num = value;
pnew_node->next = head->next;
head->next = pnew_node;
return SUCCESS;
}
void list_pop_back(STNode_def* head)
{
STNode_def* p = head;
int size = list_size(head);
int cnt = 0;
while(p->next != NULL && cnt < size-1)
{
cnt++;
p = p->next;
}
free(p->next);
p->next = NULL;
}
void list_pop_back1(STNode_def* head)
{
STNode_def* curr = head->next;
if (curr && curr->next == NULL) {
free(curr);
head->next = NULL;
return;
}
STNode_def* pre = NULL;
while(curr != NULL && curr->next != NULL)
{
pre = curr;
curr = curr->next;
}
if (pre == NULL) return;
pre->next = NULL;
free(curr);
}
void list_pop_front(STNode_def* head)
{
STNode_def* p = head->next;
if (head->next != NULL)
{
head->next = head->next->next;
free(p);
}
}
void list_reverse(STNode_def* head)
{
int size = list_size(head);
if (size <= 1)
{
return;
}
STNode_def *p = head->next;
STNode_def *ppre = head->next;
STNode_def *ptemp = NULL;
while(p != NULL)
{
ptemp = p->next;
if (head->next == p)
{
ppre = NULL;
}
p->next = ppre;
ppre = p;
p = ptemp;
}
head->next = ppre;
}
void swap(int *var1, int *var2)
{
int temp = *var1;
*var1 = *var2;
*var2 = temp;
}
void list_sort(STNode_def* head, int flag)
{
STNode_def* p = NULL;
STNode_def* q = NULL;
for (p =head->next; p->next != NULL; p = p->next)
{
for (q=head->next; q->next != NULL; q = q->next)
{
if (flag)
{
if (q->num > q->next->num)
{
swap(&(q->num), &(q->next->num));
}
}
else
{
if (q->num < q->next->num)
{
swap(&(q->num), &(q->next->num));
}
}
}
}
}
void list_destory(STNode_def* head)
{
STNode_def* p = head;
STNode_def* p_tmp = NULL;
while(p != NULL)
{
p_tmp = p;
p = p->next;
free(p_tmp);
}
}
int main()
{
STNode_def* head = list_init();
int i = 0;
for (i=0; i<10; i++)
{
list_push_back(head, i);
}
list_sort(head, 1);
printf("size:%d\n", list_size(head));
list_print(head);
list_destory(head);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
struct Node *next;
}STNode_def;
typedef struct LinkList{
struct Node head;
int size;
}STLinkList_def;
#define SUCCESS 0
#define FAILURE -1
typedef void (*PRINTLIST)(STNode_def* node);
STLinkList_def* linklist_init()
{
STLinkList_def* list_ptr = (STLinkList_def*)malloc(sizeof(STLinkList_def));
list_ptr->head.next = NULL;
list_ptr->size = 0;
return list_ptr;
}
int linklist_push_back(STLinkList_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
STNode_def* p = &(list_ptr->head);
while (p->next != NULL)
{
p = p->next;
}
p->next = data;
data->next = NULL;
list_ptr->size++;
return SUCCESS;
}
int linklist_push_front(STLinkList_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
data->next = list_ptr->head.next;
list_ptr->head.next = data;
list_ptr->size++;
return SUCCESS;
}
int linklist_push_bypos(STLinkList_def* list_ptr, int pos, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
if (pos < 0 || pos >= list_ptr->size)
{
pos = list_ptr->size;
printf("pos=%d\n", pos);
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != NULL && i<pos)
{
p = p->next;
i++;
}
data->next = p->next;
p->next = data;
list_ptr->size++;
return SUCCESS;
}
int linklist_pop_back(STLinkList_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != NULL && i < list_ptr->size-1)
{
p = p->next;
i++;
}
p->next = NULL;
list_ptr->size--;
return SUCCESS;
}
int linklist_pop_front(STLinkList_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
list_ptr->head.next = list_ptr->head.next->next;
list_ptr->size--;
return SUCCESS;
}
int linklist_pop_bypos(STLinkList_def* list_ptr, int pos)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (pos < 0 || pos > list_ptr->size -1 || list_ptr->size == 0)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != NULL && i < pos)
{
p = p->next;
i++;
}
p->next = p->next->next;
list_ptr->size--;
return SUCCESS;
}
void list_print(STLinkList_def* list_ptr, void (*PRINTLIST)(STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
STNode_def* p = &(list_ptr->head);
while (p->next != NULL)
{
p = p->next;
PRINTLIST(p);
}
}
void list_destory(STLinkList_def* list_ptr)
{
if (list_ptr == NULL)
{
return;
}
free(list_ptr);
list_ptr = NULL;
}
typedef struct Student{
STNode_def node;
char name[64];
int age;
}STStudent_def;
void student_info_print(STStudent_def* node)
{
printf("name:%s, age=%d\n", node->name, node->age);
}
int main()
{
STStudent_def p1;
STStudent_def p2;
STStudent_def p3;
STStudent_def p4;
STStudent_def p5;
strcpy(p1.name, "刘备");
strcpy(p2.name, "关羽");
strcpy(p3.name, "张飞");
strcpy(p4.name, "赵云");
strcpy(p5.name, "诸葛亮");
p1.age = 40;
p2.age = 38;
p3.age = 35;
p4.age = 30;
p5.age = 45;
STLinkList_def * list_ptr = linklist_init();
linklist_push_front(list_ptr, &p1);
linklist_push_front(list_ptr, &p2);
linklist_push_front(list_ptr, &p3);
linklist_push_front(list_ptr, &p4);
linklist_push_front(list_ptr, &p5);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
STStudent_def p6;
strcpy(p6.name, "曹操");
p6.age = 50;
linklist_push_bypos(list_ptr, 3, &p6);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
linklist_pop_bypos(list_ptr, 2);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
list_destory(list_ptr);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
typedef struct Node{
struct Node *next;
}STNode_def;
typedef struct CicLinklist{
struct Node head;
int size;
}STCicLinklist_def;
#define SUCCESS 0
#define FAILURE -1
typedef void (*PRINTLIST)(STNode_def* node);
STCicLinklist_def* ciclinklist_init()
{
STCicLinklist_def* list_ptr = (STCicLinklist_def*)malloc(sizeof(STCicLinklist_def));
list_ptr->head.next = &(list_ptr->head);
list_ptr->size = 0;
return list_ptr;
}
int ciclinklist_push_back(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
}
p->next = data;
data->next = &(list_ptr->head);
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_push_front(STCicLinklist_def* list_ptr, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
data->next = list_ptr->head.next;
list_ptr->head.next = data;
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_push_bypos(STCicLinklist_def* list_ptr, int pos, STNode_def* data)
{
if (list_ptr == NULL || data == NULL)
{
return FAILURE;
}
if (pos < 0 || pos >= list_ptr->size)
{
pos = list_ptr->size;
printf("pos=%d\n", pos);
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i<pos)
{
p = p->next;
i++;
}
data->next = p->next;
p->next = data;
list_ptr->size++;
return SUCCESS;
}
int ciclinklist_pop_back(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head) && i < list_ptr->size-1)
{
p = p->next;
i++;
}
p->next = &(list_ptr->head);
list_ptr->size--;
return SUCCESS;
}
int ciclinklist_pop_front(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (list_ptr->size == 0)
{
return FAILURE;
}
list_ptr->head.next = list_ptr->head.next->next;
list_ptr->size--;
return SUCCESS;
}
int ciclinklist_pop_bypos(STCicLinklist_def* list_ptr, int pos)
{
if (list_ptr == NULL)
{
return FAILURE;
}
if (pos < 0 || pos > list_ptr->size -1 || list_ptr->size == 0)
{
return FAILURE;
}
int i = 0;
STNode_def* p = &(list_ptr->head);
while (p->next != NULL && i < pos)
{
p = p->next;
i++;
}
p->next = p->next->next;
list_ptr->size--;
return SUCCESS;
}
void list_print(STCicLinklist_def* list_ptr, void (*PRINTLIST)(STNode_def*))
{
if (list_ptr == NULL || PRINTLIST == NULL)
{
return;
}
STNode_def* p = &(list_ptr->head);
while (p->next != &(list_ptr->head))
{
p = p->next;
PRINTLIST(p);
}
}
void list_destory(STCicLinklist_def* list_ptr)
{
if (list_ptr == NULL)
{
return;
}
free(list_ptr);
list_ptr = NULL;
}
typedef struct Student{
STNode_def node;
char name[64];
int age;
}STStudent_def;
void student_info_print(STStudent_def* node)
{
printf("name:%s, age=%d\n", node->name, node->age);
}
int main()
{
STStudent_def p1;
STStudent_def p2;
STStudent_def p3;
STStudent_def p4;
STStudent_def p5;
strcpy(p1.name, "刘备");
strcpy(p2.name, "关羽");
strcpy(p3.name, "张飞");
strcpy(p4.name, "赵云");
strcpy(p5.name, "诸葛亮");
p1.age = 40;
p2.age = 38;
p3.age = 35;
p4.age = 30;
p5.age = 45;
STCicLinklist_def * list_ptr = ciclinklist_init();
ciclinklist_push_back(list_ptr, &p1);
ciclinklist_push_back(list_ptr, &p2);
ciclinklist_push_back(list_ptr, &p3);
ciclinklist_push_back(list_ptr, &p4);
ciclinklist_push_back(list_ptr, &p5);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
STStudent_def p6;
strcpy(p6.name, "曹操");
p6.age = 50;
ciclinklist_push_bypos(list_ptr, 3, &p6);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
ciclinklist_pop_bypos(list_ptr, 2);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
list_print(list_ptr, student_info_print);
printf("size:%d\n", list_ptr->size);
list_destory(list_ptr);
return 0;
}