Skip to content

Commit 8316a86

Browse files
Merge pull request matthewsamuel95#806 from vmmc2/patch-4
Create SinglyLinkedList(Array Implementation).c
2 parents de0f634 + e5189e3 commit 8316a86

File tree

1 file changed

+127
-0
lines changed

1 file changed

+127
-0
lines changed
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
#include <stdio.h>
2+
#include <stdlib.h>
3+
#include <string.h>
4+
#include <math.h>
5+
#define MAX 10000
6+
struct List{
7+
int *array;
8+
int maxsize;
9+
int listsize;
10+
int curr;
11+
};
12+
struct List* Create_list(int tam){
13+
struct List *temp;
14+
temp = (struct List*)malloc(sizeof(struct List));
15+
temp->maxsize = tam;
16+
temp->listsize = 0;
17+
temp->curr = 0;
18+
temp->array = (int *)malloc(sizeof(int)*tam);
19+
return temp;
20+
}
21+
void Clear(struct List *temp){
22+
temp->listsize = 0;
23+
temp->curr = 0;
24+
}
25+
//A funcao vai retornar 1 se a insercao der certo.
26+
//A funcao vai retornar 0 se a insercao der errado.
27+
int Insert(struct List* temp, int x){
28+
//Na hora de printar tem que ficar ligado pra saber onde o cursor/curr se encontra na lista
29+
if(temp->listsize > temp->maxsize){
30+
return 0;
31+
}
32+
int i = temp->listsize;
33+
while(i > temp->curr){
34+
temp->array[i] = temp->array[i-1];
35+
i--;
36+
}
37+
temp->array[temp->curr] = x;
38+
temp->listsize++;
39+
return 1;
40+
}
41+
void Print(struct List *temp){
42+
int i = 0;
43+
int final = (temp->listsize)-1;
44+
for(i = 0; i <= final; i++){
45+
printf("%d\n", temp->array[i]);
46+
}
47+
}
48+
int Append(struct List* temp, int x){
49+
if(temp->listsize >= temp->maxsize){
50+
return 0;
51+
}
52+
temp->curr = temp->listsize;
53+
temp->array[temp->curr] = x;
54+
temp->listsize = temp->listsize + 1;
55+
return 1;
56+
}
57+
void MoveToStart(struct List* temp){
58+
temp->curr = 0;
59+
}
60+
void MoveToEnd(struct List* temp){
61+
temp->curr = temp->listsize;
62+
}
63+
int Prev(struct List* temp){
64+
if(temp->curr == 0){
65+
return 0;
66+
}
67+
temp->curr--;
68+
return 1;
69+
}
70+
int Next(struct List* temp){
71+
if(temp->curr >= temp->listsize){
72+
return 0;
73+
}
74+
temp->curr++;
75+
return 1;
76+
}
77+
int Length(struct List *temp){
78+
return temp->listsize;
79+
}
80+
int CurrPos(struct List* temp){
81+
return temp->curr;
82+
}
83+
int MoveToPos(struct List *temp, int pos){
84+
if(pos < 0 || pos > temp->listsize){
85+
return 0;
86+
}
87+
temp->curr = pos;
88+
return 1;
89+
}
90+
int GetValue(struct List *temp){
91+
if(temp->curr < 0 || temp->curr >= temp->listsize){
92+
return 0;
93+
}
94+
return temp->array[temp->curr];
95+
}
96+
int Delete(struct List *temp){
97+
int i;
98+
if(temp->curr < 0 || temp->curr >= temp->listsize){
99+
return 0;
100+
}
101+
i = temp->curr;
102+
while(i < temp->listsize - 1){
103+
temp->array[i] = temp->array[i+1];
104+
i++;
105+
}
106+
temp->listsize--;
107+
}
108+
109+
int main(){
110+
struct List *head = NULL;
111+
int aux;
112+
head = Create_list(10);
113+
aux = Insert(head,10);
114+
aux = Insert(head,-34);
115+
aux = Insert(head,0);
116+
aux = Insert(head,23);
117+
aux = Append(head, 69);
118+
aux = Append(head, -43);
119+
MoveToPos(head, 2);
120+
printf("A lista antes da remocao eh:\n");
121+
Print(head);
122+
Delete(head);
123+
printf("A lista depois da remocao eh:\n");
124+
Print(head);
125+
126+
return 0;
127+
}

0 commit comments

Comments
 (0)