码迷,mamicode.com
首页 > 编程语言 > 详细

链表的数组实现

时间:2015-09-23 13:19:56      阅读:197      评论:0      收藏:0      [点我收藏+]

标签:

声明 cursorLinkedList:

 1 #ifndef CURSORLINKEDLIST_H_INCLUDED
 2 #define CURSORLINKEDLIST_H_INCLUDED
 3 typedef int PtrToNode;
 4 typedef PtrToNode Position;
 5 typedef PtrToNode List;
 6 
 7 List MakeEmpty(List L);
 8 int IsEmpty(List L);
 9 int IsLast(Position P);
10 void InitializeCursorSpace();
11 void Insert(int X, Position P, List L);
12 void Delete(int X, List L);
13 Position FindPrevious(int X, List L);
14 Position Find(int X, List L);
15 static Position CursorAlloc(void);
16 static void CursorFree(Position P);
17 void DeleteList(List L);
18 Position Advance(const Position P);
19 void Print(List L);
20 
21 #endif // CURSORLINKEDLIST_H_INCLUDED

实现 implementation.c:

  1 #include<stdio.h>
  2 #include "cursorLinkedList.h"
  3 #define SpaceSize 100
  4 
  5 struct Node{
  6     int Num;
  7     Position Next;
  8 };
  9 struct Node CursorSpace[SpaceSize];
 10 
 11 static Position CursorAlloc(void) {
 12     Position P;
 13     P = CursorSpace[0].Next;
 14     CursorSpace[0].Next = CursorSpace[P].Next;
 15     return P;
 16 }
 17 
 18 static void CursorFree(Position P) {
 19     CursorSpace[P].Next = CursorSpace[0].Next;
 20     CursorSpace[0].Next = P;
 21 }
 22 
 23 List MakeEmpty(List L) {
 24     L = CursorAlloc();
 25     CursorSpace[L].Next = 0;
 26     return L;
 27 }
 28 
 29 void InitializeCursorSpace() {
 30     int i;
 31     for(i = 0; i < SpaceSize - 1; i++){
 32         CursorSpace[i].Next = i + 1;
 33         CursorSpace[i + 1].Next = 0;
 34     }
 35 }
 36 
 37 int IsEmpty(List L) {
 38     return CursorSpace[L].Next == 0;
 39 }
 40 
 41 int IsLast(Position P) {
 42     return CursorSpace[P].Next == 0;
 43 }
 44 
 45 void Insert(int X, Position P, List L) {
 46     Position TmpCell;
 47     TmpCell = CursorAlloc();
 48     if(TmpCell == 0) {
 49         printf("Out Of Space!");
 50     }
 51     CursorSpace[TmpCell].Num = X;
 52     CursorSpace[TmpCell].Next = CursorSpace[P].Next;
 53     CursorSpace[P].Next = TmpCell;
 54 }
 55 
 56 Position Find(int X, List L) {
 57     Position P = CursorSpace[L].Next;
 58     while(P != 0 && CursorSpace[P].Num != X) {
 59         P = CursorSpace[P].Next;
 60     }
 61     return P;
 62 }
 63 
 64 Position FindPrevious(int X, List L) {
 65     Position P;
 66     P = L;
 67     while(P && CursorSpace[CursorSpace[P].Next].Num != X) {
 68         P = CursorSpace[P].Next;
 69     }
 70     return P;
 71 }
 72 
 73 void Delete(int X, List L) {
 74     Position P, TmpCell;
 75     P = FindPrevious(X, L);
 76     TmpCell = CursorSpace[P].Next;
 77     CursorSpace[P].Next = CursorSpace[TmpCell].Next;
 78     CursorFree(TmpCell);
 79 }
 80 
 81 void DeleteList(List L) {
 82     Position P, TmpCell;
 83     P = CursorSpace[L].Next;
 84     CursorSpace[L].Next = 0;
 85     while(P) {
 86         TmpCell = CursorSpace[P].Next;
 87         CursorFree(P);
 88         P = TmpCell;
 89     }
 90 }
 91 
 92 Position Advance(const Position P){
 93     return CursorSpace[P].Next;
 94 }
 95 
 96 void Print(List L) {
 97     Position P = CursorSpace[L].Next;
 98     while(P) {
 99         printf("%d ", CursorSpace[P].Num);
100         P = CursorSpace[P].Next;
101     }
102     printf("\n");
103 }

 

测试 main.c:

 1 #include <stdio.h>
 2 #include <stdlib.h>
 3 #include "cursorLinkedList.h"
 4 
 5 int main()
 6 {
 7     List L;
 8     Position P;
 9     int i;
10     InitializeCursorSpace();
11     L = MakeEmpty(L);
12     P = L;
13     for(i = 1; i < 10; i++) {
14         Insert(i, P, L);
15         P = Advance(P);
16     }
17     Print(L);
18     Delete(5, L);
19     Print(L);
20     DeleteList(L);
21     Print(L);
22     printf("%d",IsEmpty(L));
23     return 0;
24 }

 

链表的数组实现

标签:

原文地址:http://www.cnblogs.com/zsdvvb/p/4831776.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!