--------------------------------------------------------------
root@xzwang-G450:/home/xzwang/wxz/struct# ./a.out
----------------Link list test demo----------------
Create link list head node
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0
See about node number:8
8
Delete the node number 1-N:9
1 2 3 4 5 6 7 8 10 0 0 0 0 0
Begin Reverse linklist:
list = 0x8e89008
...
pn = 0x8e89008, list = 0x8e89008
0 0 0 0 0 10 8 7 6 5 4 3 2 1
------------------The End-----------------
Source Code:
----------------------------------------------------------
/*
* Author: xz.wang E-mail: xz.wang@vant rontech.com. cn
* Last modified: 2013-08-11 20:15
* Filename: linklist.c
* Description:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int data;
struct node *next;
}NODE_T, * NODE_T_PTR;
/**
* @brief init_linklist @Initialze link head node
*
* @return @NULL, NODE_T *head
*/
NODE_T *init_linklist()
{
NODE_T *head = NULL;;
head = (NODE_T *)malloc(sizeof(NODE_T));
if (head == NULL) {
fprintf(stderr, "request link memory failed\n");
return NULL;
}
else
return head;
}
/**
* @brief get_linklist
*
* @param head
* @param cursor
*
* @return
*/
NODE_T *get_linklist(NODE_T *head, int cursor)
{
NODE_T *list = NULL;
if (head == NULL && cursor < 1)
return NULL;
list = head;
for (;list != NULL && cursor; cursor--) {
list = list->next;
}
return list;
}
/**
* @brief insert_linklist
*
* @param head
* @param cursor
* @param value
*
* @return
*/
int insert_linklist(NODE_T *head, int cursor, int value)
{
NODE_T *list = NULL;
NODE_T *pn;
int n = 0;
if (head == NULL && cursor < 1) {
return -1;
}
list = head;
while (list != NULL && n < cursor - 1) {
list = list->next;
n++;
}
pn = (NODE_T *)malloc(sizeof(NODE_T));
if (pn == NULL)
return -1;
list->next = pn;
pn->next = NULL;
pn->data = value;
return 0;
}
/**
* @brief delete_linklist
*
* @param head
* @param cursor
*
* @return
*/
int delete_linklist(NODE_T *head, int cursor)
{
NODE_T *list = NULL;
NODE_T *pn;
int n = 0;
if (head == NULL && cursor < 1) {
return -1;
}
list = head;
while (list != NULL && n < cursor - 1) {
list = list->next;
n++;
}
pn = list->next;
if (pn == NULL) {
fprintf(stderr, "the end, no this position\n");
}
else {
list->next = pn->next;
free(pn);
pn = NULL;
}
return 0;
}
/**
* @brief reverse_linklist
*
* @param head
*
* @return
*/
NODE_T *reverse_linklist(NODE_T *head)
{
NODE_T *pt = NULL;
NODE_T *pn, *pr;
/* begin reverse from second,not include head node */
pt = head->next;
pr = NULL;
while (pn != NULL) {
pn = pt->next;
pt->next = pr;
pr = pt;
pt = pn;
}
fprintf(stderr, "...\n");
head->next = pr;
return head;
}
/**
* @brief print_linklist
*
* @param head
*/
void print_linklist(NODE_T *head)
{
NODE_T *pn;
pn = head->next;
while (pn != NULL) {
fprintf(stderr, "%4d", pn->data);
pn = pn->next;
}
printf("\n");
}
/* ============================MAIN============================*/
int main(int argc, char *argv[])
{
int ret;
int n;
NODE_T *list = NULL, *pn;
int value[16] = {0,1,2,3,4,5,6,7,8,9,10,};
int cursor;
printf("----------------Link list test demo----------------\n");
printf("Create link list head node\n");
list = init_linklist();
if (list == NULL) {
printf("Create head node failed\n");
return 0;
}
for (n = 1; n < sizeof(value)/sizeof(value[0]); n++) {
ret = insert_linklist(list, n, value[n]);
if (ret < 0) {
printf("Insert link list failed\n");
/* FIXME, free already malloc memory */
return 0;
}
}
print_linklist(list);
printf("See about node number:");
scanf("%d", &cursor);
pn = get_linklist(list, cursor);
if (pn == NULL)
printf("Get node failed\n");
else
printf("%d\n", pn->data);
printf("Delete the node number 1-N:");
scanf("%d", &cursor);
delete_linklist(list, cursor);
print_linklist(list);
/* reverse link list */
printf("Begin Reverse linklist:\n");
pn = reverse_linklist(list);
printf("pn = %p, list = %p\n", pn, list);
print_linklist(list);
printf("------------------The End-----------------\n");
return 0;
}
root@xzwang-G450:/home/xzwang/wxz/struct# ./a.out
----------------Link list test demo----------------
Create link list head node
1 2 3 4 5 6 7 8 9 10 0 0 0 0 0
See about node number:8
8
Delete the node number 1-N:9
1 2 3 4 5 6 7 8 10 0 0 0 0 0
Begin Reverse linklist:
list = 0x8e89008
...
pn = 0x8e89008, list = 0x8e89008
0 0 0 0 0 10 8 7 6 5 4 3 2 1
------------------The End-----------------
Source Code:
----------------------------------------------------------
/*
* Author: xz.wang E-mail: xz.wang@vant
* Last modified: 2013-08-11 20:15
* Filename: linklist.c
* Description:
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct node {
int data;
struct node *next;
}NODE_T, * NODE_T_PTR;
/**
* @brief init_linklist @Initialze link head node
*
* @return @NULL, NODE_T *head
*/
NODE_T *init_linklist()
{
NODE_T *head = NULL;;
head = (NODE_T *)malloc(sizeof(NODE_T));
if (head == NULL) {
fprintf(stderr, "request link memory failed\n");
return NULL;
}
else
return head;
}
/**
* @brief get_linklist
*
* @param head
* @param cursor
*
* @return
*/
NODE_T *get_linklist(NODE_T *head, int cursor)
{
NODE_T *list = NULL;
if (head == NULL && cursor < 1)
return NULL;
list = head;
for (;list != NULL && cursor; cursor--) {
list = list->next;
}
return list;
}
/**
* @brief insert_linklist
*
* @param head
* @param cursor
* @param value
*
* @return
*/
int insert_linklist(NODE_T *head, int cursor, int value)
{
NODE_T *list = NULL;
NODE_T *pn;
int n = 0;
if (head == NULL && cursor < 1) {
return -1;
}
list = head;
while (list != NULL && n < cursor - 1) {
list = list->next;
n++;
}
pn = (NODE_T *)malloc(sizeof(NODE_T));
if (pn == NULL)
return -1;
list->next = pn;
pn->next = NULL;
pn->data = value;
return 0;
}
/**
* @brief delete_linklist
*
* @param head
* @param cursor
*
* @return
*/
int delete_linklist(NODE_T *head, int cursor)
{
NODE_T *list = NULL;
NODE_T *pn;
int n = 0;
if (head == NULL && cursor < 1) {
return -1;
}
list = head;
while (list != NULL && n < cursor - 1) {
list = list->next;
n++;
}
pn = list->next;
if (pn == NULL) {
fprintf(stderr, "the end, no this position\n");
}
else {
list->next = pn->next;
free(pn);
pn = NULL;
}
return 0;
}
/**
* @brief reverse_linklist
*
* @param head
*
* @return
*/
NODE_T *reverse_linklist(NODE_T *head)
{
NODE_T *pt = NULL;
NODE_T *pn, *pr;
/* begin reverse from second,not include head node */
pt = head->next;
pr = NULL;
while (pn != NULL) {
pn = pt->next;
pt->next = pr;
pr = pt;
pt = pn;
}
fprintf(stderr, "...\n");
head->next = pr;
return head;
}
/**
* @brief print_linklist
*
* @param head
*/
void print_linklist(NODE_T *head)
{
NODE_T *pn;
pn = head->next;
while (pn != NULL) {
fprintf(stderr, "%4d", pn->data);
pn = pn->next;
}
printf("\n");
}
/* ============================MAIN============================*/
int main(int argc, char *argv[])
{
int ret;
int n;
NODE_T *list = NULL, *pn;
int value[16] = {0,1,2,3,4,5,6,7,8,9,10,};
int cursor;
printf("----------------Link list test demo----------------\n");
printf("Create link list head node\n");
list = init_linklist();
if (list == NULL) {
printf("Create head node failed\n");
return 0;
}
for (n = 1; n < sizeof(value)/sizeof(value[0]); n++) {
ret = insert_linklist(list, n, value[n]);
if (ret < 0) {
printf("Insert link list failed\n");
/* FIXME, free already malloc memory */
return 0;
}
}
print_linklist(list);
printf("See about node number:");
scanf("%d", &cursor);
pn = get_linklist(list, cursor);
if (pn == NULL)
printf("Get node failed\n");
else
printf("%d\n", pn->data);
printf("Delete the node number 1-N:");
scanf("%d", &cursor);
delete_linklist(list, cursor);
print_linklist(list);
/* reverse link list */
printf("Begin Reverse linklist:\n");
pn = reverse_linklist(list);
printf("pn = %p, list = %p\n", pn, list);
print_linklist(list);
printf("------------------The End-----------------\n");
return 0;
}