Professional Documents
Culture Documents
GRUPI:A1
1.Te ndertohet nje algoritem qe heq nga nje tabele e dhene te gjithe
elementet qe perseriten, pa perdorur ndonje tabele shtese ndihmese.
Mund te perdoren nje ose dy variabla shtese. Cili eshte kompleksiteti i
algoritmit te ndertuar?
#include<iostream>
using namespace std;
int removeDuplicates(int a[], int n)
{
if (n==0 || n==1)
return n;
int j = 0;
for (int i=0; i < n-1; i++)
if (a[i] != a[i+1])
a[j++] = a[i];
a[j++] = a[n-1];
return j;
}
int main()
{
int a[] = {5,2,1,2,3,2,2,8};
int n = sizeof(a) / sizeof(a[0]);
n = removeDuplicates(a, n);
for (int i=0; i<n; i++)
cout << a[i] << " ";
C2 General
return 0;
}
#include <bits/stdc++.h>
using namespace std;
struct Node {
int data;
Node* next;
};
Node* getNode(int data)
{
Node* newNode = new Node;
newNode->data = data;
newNode->next = NULL;
return newNode;
}
void deleteDivisibleNodes(Node** head_ref, int P)
{
Node *temp = *head_ref, *prev;
while (temp != NULL && temp->data % P == 0) {
*head_ref = temp->next;
free(temp);
temp = *head_ref;
}
while (temp != NULL) {
C2 General
while (temp != NULL && temp->data % P != 0) {
prev = temp;
temp = temp->next;
}
C2 General
cout << "\nLista perfundimtare: ";
printList(head);
return 0;
}
C2 General
total i elementeve ne te dy stivat nuk eshte me shume sesa n. Veprimet
PUSH dhe POP te kene kohe ekzekutimi O(1).
#include <iostream>
#include <vector>
#include <cstdlib>
using namespace std;
class Ushtrimi
{
int *a;
int kapaciteti;
int t1, t2;
public:
Ushtrimi(int n)
{
kapaciteti = n;
a = new int[n];
t1 = -1;
t2 = n;
}
void pushFirst(int key)
{
if (t1 + 1 == t2)
{
cout << "Ushtrimi";
exit(EXIT_FAILURE);
}
t1++;
a[t1] = key;
}
void pushSecond(int key)
{
// check if the array is full
if (t1 + 1 == t2)
{
cout << "Ushtrimi";
exit(EXIT_FAILURE);
C2 General
}
t2--;
a[t2] = key;
}
int popFirst()
{
if (t1 < 0)
{
cout << "Ushtrimi";
exit(EXIT_FAILURE);
}
int t1 = a[t1];
t1--;
return t;
}
int popSecond()
{
if (t2 >= kapaciteti)
{
cout << "Ushtrimi";
exit(EXIT_FAILURE);
}
int t = a[t2];
t2++;
return t;
}
};
int main()
{
vector<int> a1 = { 3,5,9,1,4,7 };
vector<int> a2 = { 8,9,11,10 };
Ushtrimi(a1.size() + a2.size());
for (int i: a1) {
Ushtrimi.pushFirst(i);
}
for (int j: a2) {
C2 General
Ushtrimi.pushSecond(j);
}
cout << "Elementet e pare: " << Ushtrimi.popFirst() << endl;
cout << "Elementet e dyte: " << Ushtrimi.popSecond() << endl;
return 0;
}
5. Jepet lista e celesave 5, 28, 19, 15,20, 33, 12, 17, 10. Te vendosen ato
ne nje tabele hash me 9 elemente, duke perdorur funksionin hash h(k)
= k mod 9 dhe tekniken me lista (chaining) te trajtimit te perplasjeve.
#include <iostream>
#include <conio.h>
using namespace std;
int nrElementesh = 9;
liste *tabelaHash[nrElementesh];
int celes;
for(int i=0; i<nrElementesh; i++)
tabelaHash[i]=NULL;
C2 General
for(int i=0; i<nrElementesh; i++)
{
cin>>celes;
int vlHash = llogaritVlerenHash(celes, nrElementesh);
tabelaHash[vlHash] =
shtoElementNeFillimTeListes(tabelaHash[vlHash],celes);
}
for(int i=0; i<nrElementesh; i++)
{
liste *l = tabelaHash[i];
cout<<"elementet ne pozicionin "<<i<<" jane:";
;
while(l!=NULL)
{
cout<<l->vlere<<" ";
l = l->pas;
}
cout<<"\n";
}
return 0;
}
#include <bits/stdc++.h>
using namespace std;
class node
{
public:
int data;
node* left;
node* right;
};
C2 General
node* Node = new node();
Node->data = data;
Node->left = NULL;
Node->right = NULL;
return(Node);
}
C2 General
cout << "Pemet nuk jane identike.";
return 0;
}
#include <bits/stdc++.h>
using namespace std;
void addEdge(vector<int> adj[], int src, int dest)
{
adj[src].push_back(dest);
}
void displayGraph(vector<int> adj[], int v)
{
for (int i = 0; i < v; i++) {
cout << i << "--> ";
for (int j = 0; j < adj[i].size(); j++)
cout << adj[i][j] << " ";
cout << "\n";
}
}
/
void transposeGraph(vector<int> adj[],
vector<int> transpose[], int v)
{
/
for (int i = 0; i < v; i++)
for (int j = 0; j < adj[i].size(); j++)
addEdge(transpose, adj[i][j], i);
}
C2 General
int main()
{
int v = 5;
vector<int> adj[v];
addEdge(adj, 0, 1);
addEdge(adj, 0, 3);
addEdge(adj, 0, 4);
addEdge(adj, 2, 0);
addEdge(adj, 5, 2);
addEdge(adj, 4, 2);
addEdge(adj, 1, 2);
vector<int> transpose[v];
transposeGraph(adj, transpose, v);
displayGraph(transpose, v);
return 0;
}
C2 General