码迷,mamicode.com
首页 > 其他好文 > 详细

数据结构学习第三天

时间:2019-08-18 23:55:29      阅读:165      评论:0      收藏:0      [点我收藏+]

标签:单链表   ptr   open   eem   结构   enqueue   第三天   dispose   insert   

19:28:16 2019-08-18

今天稍微早点。

 

双链表

DList.h

技术图片
 1 #ifndef _DLIST_H
 2 #define _DLIST_H
 3 #define len sizeof(struct Node)
 4 #include<malloc.h>
 5 struct  Node;
 6 typedef struct Node* PtrToNode;
 7 typedef PtrToNode DList;
 8 typedef PtrToNode DPosition;
 9 DList MakeEmety(DList L);
10 int IsEmpty(DList L);
11 int IsLast(DPosition P, DList L);
12 DPosition Find(int Element, DList L);
13 void Delete(int Element, DList L);
14 DPosition FindPrevious(int Element, DList L);
15 void InsertBefore(int Element,DList L, DPosition P);
16 void InsertAfter(int Element, DList L, DPosition P);
17 void DeleteList(DList L);
18 DPosition Header(DList L);
19 DPosition First(DList L);
20 #endif // !_DLIST_H
View Code

DList.cpp

技术图片
  1 #include"DList.h"
  2 
  3 struct  Node
  4 {
  5     int Element;
  6     DPosition Prior;
  7     DPosition Next;
  8 };
  9 
 10 DList MakeEmety(DList L)
 11 {
 12     L = (DList)malloc(len);
 13     L->Prior = NULL;
 14     L->Next = NULL;
 15     return L;
 16 }
 17 
 18 int IsEmpty(DList L)
 19 {
 20     return L->Next == NULL;
 21 }
 22 
 23 int IsLast(DPosition P, DList L)
 24 {
 25     return P->Next == NULL;
 26 }
 27 
 28 DPosition Find(int Element, DList L)
 29 {
 30     DPosition P = L->Next;
 31     while (P!=NULL)
 32     {
 33         if (P->Element == Element)
 34             return P;
 35         else
 36             P = P->Next;
 37     }
 38     return NULL;
 39 }
 40 
 41 void Delete(int Element, DList L)
 42 {
 43     DPosition P = FindPrevious(Element, L);
 44     P->Next = P->Next->Next;
 45     P->Next->Prior = P;
 46 }
 47 
 48 DPosition FindPrevious(int Element, DList L)
 49 {
 50     DPosition P = L;
 51     while (P->Next!=NULL)
 52     {
 53         if (P->Next->Element==Element)
 54             return P;
 55         else
 56             P = P->Next;
 57     }
 58 }
 59 
 60 void InsertBefore(int Element, DList L, DPosition P)
 61 {
 62     DPosition P1 = (DPosition)malloc(len);
 63     P1->Element=Element;
 64     P1->Prior = P->Prior;
 65     P1->Next = P;
 66     P->Prior->Next = P1;
 67     P->Prior = P1;
 68 }
 69 
 70 void InsertAfter(int Element, DList L, DPosition P)
 71 {
 72     DPosition P1 = (DPosition)malloc(len);
 73     P1->Element = Element;
 74     P1->Prior = P;
 75     P1->Next = P1->Next;
 76     P->Next = P1;
 77     P1->Next->Prior = P1;   //如果是插在末尾 这句话要删掉
 78 }
 79 
 80 void DeleteList(DList L)
 81 {
 82     DPosition P1, P2;
 83     P1 = P2 = L->Next;
 84     while (P2 != NULL)
 85     {
 86         P2 = P1->Next;
 87         free(P1);
 88         P1 = P2;
 89     }
 90 }
 91 
 92 DPosition Header(DList L)
 93 {
 94     return L;
 95 }
 96 
 97 DPosition First(DList L)
 98 {
 99     return L->Next;
100 }
View Code

 

循环单链表是将末尾元素中的指针指向第一个节点

循环双链表是将末尾元素中的next指针指向第一个节点 ,第一个节点中的prior指针指向最后一个节点

 

栈的链表实现

LIFO(后进先出)

Stack.h

技术图片
 1 #ifndef _STACK_H
 2 #define _STACK_H
 3 #define len sizeof(struct Node)
 4 struct Node;
 5 typedef struct Node* PtrToNode;
 6 typedef PtrToNode Stack;
 7 int IsEmpty(Stack S);
 8 Stack CreatStack();
 9 void DisposeStack(Stack S);
10 void MakeEmpty(Stack S);
11 void Push(int Element, Stack S);
12 int Top(Stack S);
13 void Pop(Stack S);
14 #endif // !_STACK_H
View Code

Stack.cpp

技术图片
 1 #include"Stack.h"
 2 #include<stdio.h>
 3 #include<malloc.h>
 4 struct  Node
 5 {
 6     int Element;
 7     PtrToNode Next;
 8 };
 9 
10 int IsEmpty(Stack S)
11 {
12     return    S->Next == NULL;
13 }
14 
15 Stack CreatStack()
16 {
17     Stack S = (Stack)malloc(len);
18     S->Next = NULL;
19     MakeEmpty(S);
20     return S;
21 }
22 
23 void DisposeStack(Stack S)
24 {
25     while (!IsEmpty(S))
26     {
27         Pop(S);
28     }
29     free(S);
30 }
31 
32 void MakeEmpty(Stack S)
33 {
34     while (!IsEmpty(S))
35     {
36         Pop(S);
37     }
38 }
39 
40 void Push(int Element, Stack S)
41 {
42     PtrToNode P = (PtrToNode)malloc(len);
43     P->Element = Element;
44     P->Next = S->Next;
45     S->Next = P;
46 }
47 
48 int Top(Stack S)
49 {
50     if (IsEmpty(S))
51     {
52         printf("No Element");
53         return -1;
54     }
55     return S->Next->Element;
56 }
57 
58 void Pop(Stack S)
59 {
60     PtrToNode P=S->Next;
61     S->Next = S->Next->Next;
62     free(P);
63 }
View Code

 

队列的链表实现

Queue.h

技术图片
 1 #ifndef _QUEUE_H
 2 #define _QUEUE_H
 3 #include<malloc.h>
 4 #define  len sizeof(Node)
 5 #define length sizeof(Queue)
 6 struct  Node;
 7 struct  Queue;
 8 typedef struct Node* PtrToNode;
 9 typedef struct Queue* HQueue;
10 int IsEmpty(HQueue H);
11 HQueue CreatQueue();
12 void DiposeQueue(HQueue H);
13 void Enqueue(int Element, HQueue H);
14 void Dequeue(HQueue H);
15 #endif // !_QUEUE_H
View Code

Queue.cpp

技术图片
 1 #include<stdio.h>
 2 #include"Queue.h"
 3 
 4 struct  Node
 5 {
 6     int Element;
 7     PtrToNode Next;
 8 };
 9 struct Queue
10 {
11     PtrToNode front;
12     PtrToNode rear;
13 };
14 
15 int IsEmpty(HQueue H)
16 {
17     return H->rear == NULL || H->front == NULL;
18 }
19 
20 HQueue CreatQueue()
21 {
22     HQueue H= (HQueue)malloc(length);
23     H->front = NULL;
24     H->rear = NULL;
25 }
26 
27 void DiposeQueue(HQueue H)
28 {
29     while (!IsEmpty(H))
30     {
31         Queue(H);
32     }
33     free(H);
34 }
35 
36 void Enqueue(int Element, HQueue H)
37 {
38     PtrToNode P = (PtrToNode)malloc(len);
39     P->Element = Element;
40     if (IsEmpty(H))
41     {
42         H->front = P;
43         H->rear = P;
44     }
45     else
46     {
47         H->rear->Next = P;
48         H->rear = P;
49     }
50 }
51 
52 void Dequeue(HQueue H)
53 {
54     PtrToNode P;
55     if (IsEmpty(H))
56         return;
57     else
58     {
59         P = H->front;
60         /*H->front = H->front->Next;   //当队列中只有一个元素时候要特殊处理
61         free(P);*/
62     }    
63     if (H->front== H->rear)
64     {
65         free(P);
66         H->front = H->rear = NULL;
67     }
68     else
69     {
70         H->front = H->front->Next;
71         free(P);
72     }
73 }
View Code

 

数据结构学习第三天

标签:单链表   ptr   open   eem   结构   enqueue   第三天   dispose   insert   

原文地址:https://www.cnblogs.com/57one/p/11374474.html

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