歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言學習之單向鏈表操作

C語言學習之單向鏈表操作

日期:2017/3/1 9:33:29   编辑:Linux編程

該文件為單向鏈表操作的一些接口:(如發現有錯誤的地方,及時告知,不勝感激!)

list.h

#ifndef _CHAINLIST_H_
#define _CHAINLIST_H_

typedef struct
{
char key[15];
char name[20];
int age;
}DATATYPE_T;

typedef struct Node
{
DATATYPE_T data;
struct Node *next;
}chainListType;

/* 添加節點到鏈表末尾 */
chainListType *chainlist_add_end(chainListType *head,DATATYPE_T data);

/* 添加節點到鏈表首部 */
chainListType *chainlist_add_first(chainListType *head,DATATYPE_T data);

/* 按關鍵字在鏈表中查找內容 */
chainListType *chainlist_find(chainListType *head,char *key);

/* 插入節點到鏈表指定位置 */
chainListType *chainlist_insert(chainListType *head,char *findkey,DATATYPE_T data);

/* 刪除指定關鍵字的節點 */
chainListType *chainlist_delete(chainListType *head,char *key);

/*獲取鏈表的節點數量 */
int chainlist_length(chainListType *head);


#endif

list.c文件如下:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include "chainlist.h"

chainListType *chainlist_add_end(chainListType *head,DATATYPE_T data)
{
chainListType *node,*phead;

node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}
node->data = data;
node->next = NULL;
if(head ==NULL)
{
head = node;
return head;
}
phead = head;

while(phead->next != NULL)
{
phead = phead->next;
}
phead->next = node;

return head;
}

chainListType *chainlist_add_first(chainListType *head,DATATYPE_T data)
{
chainListType *node;

node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}
node->data = data;
node->next = head;
head = node;

return head;
}

chainListType *chainlist_find(chainListType *head,char *key)
{
chainListType *phead;
phead = head;

while(phead)
{
if(strcmp(phead->data.key,key)==0)
{
return phead;
}
phead = phead->next;
}
return NULL;
}

chainListType *chainlist_insert(chainListType *head,char *findkey,DATATYPE_T data)
{
chainListType *node,*node1;

node = malloc(sizeof(chainListType));
if(NULL == node)
{
printf("malloc failed\n");
return NULL;
}

node->data = data;
node1 = chainlist_find(head,findkey);
if(node1)
{
node1->next = node->next;
node1->next = node;
}
else
{
free(node);
printf("can't find key\n");
}
return head;
}

chainListType *chainlist_delete(chainListType *head,char *key)
{
chainListType *node,*phead;
node = head;
phead = head;

while(phead)
{
if(strcmp(head->data.key,key)==0)
{
node = head->next;
free(head);
head = NULL;
head = node;
if(head!=NULL)
{
return head;
}
else
{
return NULL;
}
}
if(strcmp(phead->data.key,key)==0)
{
node->next = phead->next;
free(phead);
phead = NULL;
return head;
}
else
{
node = phead;
phead = phead->next;
}
}
return NULL;
}

int chainlist_length(chainListType *head)
{
int length = 0;
chainListType *phead;

phead = head;
while(phead)
{
phead = phead->next;
length++;
}

return length;
}

C語言之單向鏈表 http://www.linuxidc.com/Linux/2015-01/111246.htm

C++ 隱式類類型轉化 Implicit Class-Type Conversions http://www.linuxidc.com/Linux/2013-01/78071.htm

C語言變長數組之剖析 http://www.linuxidc.com/Linux/2013-07/86997.htm

C語言需要注意的問題 http://www.linuxidc.com/Linux/2013-05/84301.htm

C語言位域的使用及其注意點 http://www.linuxidc.com/Linux/2013-07/87027.htm

C語言中簡單的for循環和浮點型變量 http://www.linuxidc.com/Linux/2013-08/88514.htm

Copyright © Linux教程網 All Rights Reserved