歡迎來到Linux教程網
Linux教程網
Linux教程網
Linux教程網
Linux教程網 >> Linux編程 >> Linux編程 >> C語言線性表(基於鏈式結構)

C語言線性表(基於鏈式結構)

日期:2017/3/1 9:41:09   编辑:Linux編程

C語言線性表(基於鏈式結構)

List.h文件

/**
* C語言線性表(基於鏈式結構)
* 指定數據類型為整型
* 采用頭結點方式
*/
#define TRUE 1
#define FALSE 0
#define OK 1
#define NO 0

typedef int Status;
typedef int ElemType;
//定義表節點的結構
typedef struct Node{
ElemType data;
struct Node* next;
}Node;
//定義表的結構
typedef struct Node* List;

/*
* 初始化線性表
*/
List InitList(int n);
/*
* 銷毀線性表
*/
void DestroyList(List l);
/*
* 清空線性表
*/
void ClearList(List l);
/*
* 判斷線性表是否為空
*/
Status ListEmpty(List l);
/*
* 計算線性表中元素的個數
*/
int ListLength(List l);
/*
* 獲得指定位置元素
*/
Status GetElem(List l, int i, ElemType* e);
/*
* 獲得指定元素位置
*/
int LocateElem(List l, ElemType e);
/*
* 獲取指定元素的前一個元素
*/
Status PriorElem(List l, int i, ElemType* e);
/*
* 獲取指定元素的後一個元素
*/
Status NextElem(List l, int i, ElemType* e);
/*
* 向線性表指定位置插入一個元素
*/
Status ListInsert(List l, int i, ElemType e);
/*
* 刪除線性表指定位置的元素
*/
Status ListDelete(List l, int i);
/*
* 輸出鏈表內所有元素的值
*/
void PrintList(List l);

List.c文件

#include <stdio.h>
#include <stdlib.h>
#include "List.h"

List InitList(int n)
{
//{
/*
* 以下是一種正常的初始化方法,為了進一步提高效率還有兩種改進方法頭插法和尾插法,
* 這兩種方法可以減少循環中一次復制運算,並減少了定義臨時指針
*/
List l; //定義一個節點指針,用於保存鏈表第一個節點的地址
l = (List)malloc(sizeof(Node)); //定義一個節點作為頭節點並賦給節點指針
l->next = NULL; //初始化頭結點

Node *t; //定義一個節點指針,用於保存臨時地址
t = l; //將頭結點的地址賦給指針

int i;
for(i=1; i<=n; i++){
//創建一個新節點並初始化
Node* p = (Node*)malloc(sizeof(Node));
p->data = i;
p->next = NULL;
t->next = p; //另上個節點的next指針指向p;
t = p; //讓t保存新節點p的地址
}

return l;
//}
}

void DestroyList(List l)
{
Node* t;
while(l){
t = l;
l = l->next;
free(t);
}
l = NULL;
}

void ClearList(List l)
{
l = l->next;
while(l){
l->data = 0;
l = l->next;
}
}

Status GetElem(List l, int i, ElemType* e)
{
while(l && i){
l = l->next;
i--;
}
if(i != 0)
return NO;
*e = l->data;
return OK;
}

Status ListEmpty(List l)
{
if(l)
return FALSE;
else
return TRUE;
}

int ListLength(List l)
{
int length = 0;
while(l){
l = l->next;
length++;
}
return length;
}

int LocateElem(List l, ElemType e)
{
l = l->next;
int location = 0;
while(l){
location++;
if(l->data == e)
return location;
l = l->next;
}
return 0;
}

Status PriorElem(List l, int i, ElemType* e)
{
int j = 1;
l = l->next;
Node* tmp_node;
while(l && j<i){
tmp_node = l;
l = l->next;
j++;
}
if(j < i)
return FALSE;
else{
*e = tmp_node->data;
return TRUE;
}
}

Status NextElem(List l, int i, ElemType* e)
{
while(l && i){
l = l->next;
i--;
}
if(i != 0)
return FALSE;
else{
if(l->next){
*e = l->next->data;
return TRUE;
}else{
return FALSE;
}
}
}

Status ListInsert(List l, int i, ElemType e)
{
l = l->next;
Node* tmp_node;
while(l && i){
tmp_node = l;
l = l->next;
i--;
}
if(i != 0)
return FALSE;
else{
Node* p = (Node*)malloc(sizeof(Node));
p->data = e;
p->next = l;
tmp_node->next = p;
return TRUE;
}

}

Status ListDelete(List l, int i)
{
Node* tmp_node;
while(l && i){
tmp_node = l;
l = l->next;
i--;
}
if(i != 0)
return TRUE;
else{
tmp_node->next = l->next;
free(l);
return TRUE;
}
}

void PrintList(List l)
{
l = l->next;
while(l){
printf("%d\n",l->data);
l = l->next;
}
}

int main()
{
List l = InitList(5);
ElemType tmp_elem = 0;

PrintList(l);

int s = GetElem(l,3,&tmp_elem);
printf("the element is %d\n", tmp_elem);

NextElem(l,3,&tmp_elem);
printf("the goal element's next element is %d\n", tmp_elem);

if(PriorElem(l,3,&tmp_elem))
printf("the goal element's prior element is %d\n", tmp_elem);

int location = LocateElem(l,4);
printf("the location id is %d\n", location);

ListInsert(l,3,8);
PrintList(l);

ListDelete(l,4);
PrintList(l);

return 0;
}

C++ Primer Plus 第6版 中文版 清晰有書簽PDF+源代碼 http://www.linuxidc.com/Linux/2014-05/101227.htm

讀C++ Primer 之構造函數陷阱 http://www.linuxidc.com/Linux/2011-08/40176.htm

讀C++ Primer 之智能指針 http://www.linuxidc.com/Linux/2011-08/40177.htm

讀C++ Primer 之句柄類 http://www.linuxidc.com/Linux/2011-08/40175.htm

將C語言梳理一下,分布在以下10個章節中:

  1. Linux-C成長之路(一):Linux下C編程概要 http://www.linuxidc.com/Linux/2014-05/101242.htm
  2. Linux-C成長之路(二):基本數據類型 http://www.linuxidc.com/Linux/2014-05/101242p2.htm
  3. Linux-C成長之路(三):基本IO函數操作 http://www.linuxidc.com/Linux/2014-05/101242p3.htm
  4. Linux-C成長之路(四):運算符 http://www.linuxidc.com/Linux/2014-05/101242p4.htm
  5. Linux-C成長之路(五):控制流 http://www.linuxidc.com/Linux/2014-05/101242p5.htm
  6. Linux-C成長之路(六):函數要義 http://www.linuxidc.com/Linux/2014-05/101242p6.htm
  7. Linux-C成長之路(七):數組與指針 http://www.linuxidc.com/Linux/2014-05/101242p7.htm
  8. Linux-C成長之路(八):存儲類,動態內存 http://www.linuxidc.com/Linux/2014-05/101242p8.htm
  9. Linux-C成長之路(九):復合數據類型 http://www.linuxidc.com/Linux/2014-05/101242p9.htm
  10. Linux-C成長之路(十):其他高級議題

Copyright © Linux教程網 All Rights Reserved