-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLeftShift.c
More file actions
115 lines (73 loc) · 1.75 KB
/
Copy pathLeftShift.c
File metadata and controls
115 lines (73 loc) · 1.75 KB
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
#include <stdio.h>
#include <stdlib.h>
struct array{
int *arrptr;
int length;
int size;
};
void shift(struct array*,int);
void output(struct array*);
void input(struct array*);
struct array* createArray(int);
void rotate(struct array*,int);
int main(void)
{
struct array *A;
int size,shifts,rotates;
printf("Input the size of the array !\n");
scanf("%d", &size);
A=createArray(size);
input(A);
printf("Input the number of rotates you want!\n");
scanf("%d",&rotates);
// shift(A,shifts);
rotate(A,rotates);
output(A);
free(A);
return 0;
}
struct array* createArray(int size){
struct array *p;
p=(struct array*)malloc(sizeof(struct array));
p->arrptr=(int*)malloc(sizeof(int)*size);
p->length=0;
p->size=size;
return p;
}
void input(struct array *p){
printf("Input the number of elements you want to input to the array!\n");
scanf("%d",&p->length);
printf("Input the elements :\n");
for (size_t i = 0; i < p->length; i++)
{
scanf("%d",p->arrptr+i);
}
}
void output(struct array *p){
for (size_t i = 0; i < p->length; i++)
{
printf("%d ",*(p->arrptr+i));
}
printf("\n\n");
}
void shift(struct array *p,int shifts){
for(int j=0;j<shifts;++j){
for (size_t i = 0; i < p->length; i++)
{
*(p->arrptr+i)=*(p->arrptr+i+1);
}
*(p->arrptr+p->length-1)=0;
}
}
void rotate(struct array *p,int rotate){
int temp=0;
for (size_t i = 0; i < rotate; i++)
{
temp=*(p->arrptr);
for (size_t i = 0; i < p->length; i++)
{
*(p->arrptr+i)=*(p->arrptr+i+1);
}
*(p->arrptr+p->length-1)=temp;
}
}