#include<string.h>
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
struct node* next;
int data;
}node_t;
typedef struct list
{
int len;
node_t* head;
}list_t;
node_t* creatnode(int vdata)
{
node_t* pnode = malloc(sizeof(node_t));
if (pnode)
{
pnode->data = vdata;
pnode->next = NULL;
return pnode;
}
return NULL;
}
void destroy_node(node_t* pnode)
{
if (pnode)
{
free(pnode);
}
}
list_t* creatlist()
{
list_t* plist = malloc(sizeof(list_t));
if (plist)
{
plist->len = 0;
plist->head = NULL;
return plist;
}
return NULL;
}
void destroy_list(list_t* plist)
{
node_t* cur_node = NULL;
node_t* tmp_node = NULL;
if (plist)
{
cur_node = plist->head;
while (cur_node)
{
tmp_node = cur_node;
destroy_node(tmp_node);
cur_node = cur_node->next;
}
free(plist);
}
}
void insert_pos(list_t *plist, int pos, int data)
{
node_t* cur_node = NULL;
node_t* head = NULL;
node_t* tar_node = NULL;
int i=1;
if (NULL == plist)
return;
tar_node = creatnode(data);
if (NULL == tar_node)
return;
head = plist->head;
if (NULL == head)
{
plist->head = tar_node;
tar_node->next = tar_node;
plist->len++;
return;
}
cur_node = head;
if (1 == pos)
{
while (cur_node->next != head)
{
cur_node = cur_node->next;
}
cur_node->next = tar_node;
tar_node->next = head;
plist->head = tar_node;
}
else
{
while (cur_node->next != head && pos != i)
{
i++;
cur_node = cur_node->next;
}
if (cur_node->next == head)
tar_node->next = plist->head;
else
tar_node->next = cur_node->next;
cur_node->next = tar_node;
}
plist->len++;
return;
}
void insert_tail(list_t *plist, int data)
{
node_t* cur_node = NULL;
node_t* head = NULL;
node_t* tar_node = NULL;
if (NULL == plist)
return;
tar_node = creatnode(data);
if (NULL == tar_node)
return;
head = plist->head;
if (NULL == head)
{
plist->head = tar_node;
tar_node->next = tar_node;
plist->len++;
return;
}
cur_node = head;
while (cur_node->next != head)
{
cur_node = cur_node->next;
}
tar_node->next = plist->head;
cur_node->next = tar_node;
plist->len++;
return;
}
void del_pos(list_t *plist, int pos)
{
node_t* cur_node = NULL;
node_t* head = NULL;
node_t* pre_node = NULL;
int i=1;
if (NULL == plist)
return;
head = plist->head;
if (NULL == head || plist->len < pos || pos < 0)
{
return;
}
cur_node = head;
if (1 == pos)
{
if (1==plist->len)
{
free(head);
plist->head = NULL;
plist->len--;
return;
}
while (cur_node->next != head)
{
cur_node = cur_node->next;
}
plist->head = head->next;
cur_node->next = plist->head;
free(head);
}
else
{
while (cur_node->next != head && pos != i)
{
i++;
pre_node = cur_node;
cur_node = cur_node->next;
}
if (cur_node->next == head)
pre_node->next = plist->head;
else
pre_node->next = cur_node->next;
free(cur_node);
}
plist->len--;
return;
}
int findval(list_t *plist, int val)
{
node_t *cur_node = NULL;
int i=1;
if (NULL == plist)
return;
cur_node = plist->head;
while (cur_node->next != plist->head && val != cur_node->data) //判断不到尾节点
{
i++;
cur_node = cur_node->next;
}
if (cur_node->next == plist->head && val != cur_node->data)
return -1;
else
return i;
}
void printflist(list_t *plist)
{
node_t *p_Node = NULL;
if (NULL == plist || NULL == plist->head)
return;
printf("*******************\n");
p_Node = plist->head; //按顺序打印节点
while(NULL!=p_Node->next && p_Node->next != plist->head)
{
printf("p_Node value = %2d\n",p_Node->data);
p_Node = p_Node->next;
}
printf("p_Node value = %2d\n",p_Node->data);
printf("plist len = %2d\n", plist->len);
printf("*******************\n");
}
int main(int argc, char * argv [ ])
{
int index = 0;
int value = 0;
list_t *plist = creatlist();
node_t *p_Node = NULL;
if (NULL ==plist)
return -1;
for(index = 0; index < 10; index++)
{
value = index * 2;
insert_tail(plist, value);
printf("plist len = %2d, index = %2d, value = %2d\n", plist->len, index, value);
}
printflist(plist);
for(index = 5; index < 15; index++)
{
value = index * 10;
insert_pos(plist, index ,value);
printf("plist len = %2d, index = %2d, value = %2d\n", plist->len, index, value);
}
printflist(plist);
printf("findval %d\n", findval(plist, 222));
for(index = 22; index >0; index--)
{
del_pos(plist, index);
printflist(plist);
}
return 1;
}