-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhlist.c
119 lines (93 loc) · 1.72 KB
/
hlist.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
#include<stdio.h>
#include<stdlib.h>
#include<ncurses.h>
#include <errno.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <fcntl.h>
#include "hlist.h"
void init_hlist(hlist *hl) {
hl->head = NULL;
hl->rear = NULL;
}
int hlength (hlist hl) {
hnode *temp;
temp = hl.head;
if(temp == NULL) {
return 0;
}
int hlen = 1;
if(temp == hl.rear){
return hlen;
}
do {
temp = temp->next;
hlen++;
}while(temp != hl.rear);
return hlen;
}
void hinsert (hlist *hl, char c, int pos) {
hnode *temp, *new_hnode;
int hlen, i = 0;
hlen = hlength(*hl);
if (pos <0 || pos > hlen)
return;
new_hnode = (hnode*)malloc(sizeof(hnode));
new_hnode->data = c;
if(hlen == 0) {
hl->head = new_hnode;
hl->rear = new_hnode;
new_hnode->prev = NULL;
new_hnode->next = NULL;
return;
}
if(pos == 0) {
new_hnode->next = hl->head;
new_hnode->prev = NULL;
hl->head->prev = new_hnode;
hl->head = new_hnode;
return;
}
if (pos == hlen) {
new_hnode->prev = hl->rear;
new_hnode->next = NULL;
hl->rear->next = new_hnode;
hl->rear = new_hnode;
return;
}
temp = hl->head;
for(i = 0; i < pos - 1; i++) {
temp = temp->next;
}
new_hnode->prev = temp;
new_hnode->next = temp->next;
temp->next->prev = new_hnode;
temp->next = new_hnode;
}
void print_hlist(hlist hl) {
hnode *temp;
temp = hl.head;
if(temp == NULL) {
return ;
}
if(temp == hl.rear){
printw("%c", temp->data);
return;
}
do {
printw("%c", temp->data);
temp = temp->next;
}while(temp != NULL);
}
void hbreak(hlist *hl, int x){
int i;
hnode *temp;
temp = hl->head;
for(i = 0;i < x-1; i++){
temp = temp->next;
}
hl->head = temp->next;
hl->head->prev = NULL;
temp->next = NULL;
}