#include <iostream>
using namespace std;
#define N 11
int queuedata[N];
int front = 0;
int rear = 0;
int add(int data)
{
if (rear < N - 1)
{
rear++;
queuedata[rear] = data;
if (!front)
return -1;
return 1;
}
return -1;
}
int get()
{
int temp;
if (front)
{
temp = queuedata[front];
if (front > rear)
{
front = 0;
rear = 0;
return -1;
}
else
front++;
}
else
return -1;
return temp;
}
void show()
{
for (int i = front+1; i < rear + 1; i++)
{
cout << queuedata[i] << " ";
}
cout << endl;
}
int main(int argc, char** argv)
{
add(5);
add(8);
get();
show();
return 0;
}
วันอาทิตย์ที่ 11 มิถุนายน พ.ศ. 2560
C++ stack
#include <iostream>
using namespace std;
#define N 10
int stackdata[N];
int top = -1;
int push(int n)
{
if (top < N - 1)
{
top++;
stackdata[top] = n;
return 1;
}
return -1;
}
int pop()
{
int r;
if (top > -1)
{
r = stackdata[top];
stackdata[top] = 0;
top--;
return r;
}
return -1;
}
int main(int argc, char** argv)
{
push(1);
push(3);
pop();
push(9);
push(9);
push(3);
for (int i = 0; i <= top; i++)
{
cout << stackdata[i] << " ";
}
cout << endl;
return 0;
}
using namespace std;
#define N 10
int stackdata[N];
int top = -1;
int push(int n)
{
if (top < N - 1)
{
top++;
stackdata[top] = n;
return 1;
}
return -1;
}
int pop()
{
int r;
if (top > -1)
{
r = stackdata[top];
stackdata[top] = 0;
top--;
return r;
}
return -1;
}
int main(int argc, char** argv)
{
push(1);
push(3);
pop();
push(9);
push(9);
push(3);
for (int i = 0; i <= top; i++)
{
cout << stackdata[i] << " ";
}
cout << endl;
return 0;
}
วันเสาร์ที่ 10 มิถุนายน พ.ศ. 2560
C++ การเรียงกลับหลังใน Array
#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int i;
for (i = 0; i < N / 2; i++)
{
int temp;
temp = data[i];
data[i] = data[N - 1 - i];
data[N - 1 - i] = temp;
}
for (int i = 0; i < N; i++)
cout << data[i] << " ";
cout << endl;
return 0;
}
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int i;
for (i = 0; i < N / 2; i++)
{
int temp;
temp = data[i];
data[i] = data[N - 1 - i];
data[N - 1 - i] = temp;
}
for (int i = 0; i < N; i++)
cout << data[i] << " ";
cout << endl;
return 0;
}
C++ การลบข้อมูลใน Array
#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int position = 3;
int i;
for (i = position; i < N; i++)
data[i - 1] = data[i];
data[i - 1] = -1;
for (int i = 0; i < N; i++)
cout << data[i] << " ";
cout << endl;
return 0;
}
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int position = 3;
int i;
for (i = position; i < N; i++)
data[i - 1] = data[i];
data[i - 1] = -1;
for (int i = 0; i < N; i++)
cout << data[i] << " ";
cout << endl;
return 0;
}
C++ การแทรกข้อมูลใน Array
#include <iostream>
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int newdata = 6;
int position = 3;
int i;
for (i = N - 1; i >= position; i--)
data[i] = data[i - 1];
data[i] = newdata;
for (int i = 0; i < N; i++)
{
cout << data[i] << " ";
}
cout << endl;
return 0;
}
using namespace std;
#define N 5
int main(int argc, char** argv)
{
int data[N] = { 1,2,3,4,5 };
int newdata = 6;
int position = 3;
int i;
for (i = N - 1; i >= position; i--)
data[i] = data[i - 1];
data[i] = newdata;
for (int i = 0; i < N; i++)
{
cout << data[i] << " ";
}
cout << endl;
return 0;
}
C++ ค้นหาข้อมูลใน Array
#include <iostream>
using namespace std;
int main(int argc, char** argv)
{
int data[5] = { 1,2,3,4,5 };
int count = 0;
bool h = false;
int search = 5;
while (count < 5)
{
if (data[count] == search)
{
cout << "Have data at position " << count+1 << endl;
h = true;
break;
}
count++;
}
if (h == false)
cout << "Have not data in array" << endl;
return 0;
}
using namespace std;
int main(int argc, char** argv)
{
int data[5] = { 1,2,3,4,5 };
int count = 0;
bool h = false;
int search = 5;
while (count < 5)
{
if (data[count] == search)
{
cout << "Have data at position " << count+1 << endl;
h = true;
break;
}
count++;
}
if (h == false)
cout << "Have not data in array" << endl;
return 0;
}
สมัครสมาชิก:
บทความ (Atom)