Stacks Using Linked List - stack

#include<stdio.h>
#include<stdlib.h>
struct node{
char data;
struct node*next;
};
struct node* top =NULL;
void push(char x){
struct node* temp =(struct node*)malloc(sizeof(struct node));
if(temp==NULL){
printf("Stacks Overflow\n");
return;
}
else{
temp->data = x;
temp->next = top;
top = temp;
}
}
char pop(){
if(top==NULL){
printf("Stacks Underflow\n");
return -1;
}
else{
char x;
struct node* p = top;
top = top->next;
x = p->data;
free(p);
return x;
}
}
char peek(int pos){
struct node*p = top;
int i;
for(i=0;p!=NULL && i<pos-1;i++)
p = p->next;
return (p!=NULL)?p->data:-1;
}
int main(){
char n, x;
printf("Enter the Number of Character to be pushed\n");
scanf("%d", &n);
for(char i=0;i<n;i++){
printf("Enter the Character\n");
x = getchar();
push(x);
}
while(top){
printf("%d,", pop());
}
}
why this code not work with char(n, x) but work fine with int(x, n).
I tried to implement char stack using linked list.
It works fine with integer but doesnot work with character.
When I use to insert char, one input get skipped but this doesnot happen in int .. Why?

Related

openmp spmv CSR vectorization

I've produced this kernel
struct csrFormat {
int M, N;
int *IRP;
int *JA;
double *AS;
};
struct vector {
int dim;
double *val;
};
double csrSIMDReduction(struct csrFormat *csr, struct vector *vec, struct vector *res, int nz){
int M = csr->M;
int nonzerosPerRow = nz/M;
int chunk_size = 10000/nonzerosPerRow;
double *val = res->val;
struct timeval start,end;
gettimeofday(&start, NULL);
//IRP is the row_pointer array
//JA is the column indices array
//AS is the values array
int i,j,tmp;
#pragma omp parallel
{
#pragma omp for private(i, j, tmp) schedule(dynamic, chunk_size)
for (i=0; i<M; i++)
{
double result = 0.0;
#pragma omp simd reduction(+ : result)
for (j = csr->IRP[i]; j < csr->IRP[i+1]; j++)
{
tmp = csr->JA[j];
result += csr->AS[j] * vec->val[tmp];
}
val[i] = result;
}
}
gettimeofday(&end, NULL);
res->dim = M;
//printf("%ld.%06ld\n", start.tv_sec, start.tv_usec);
//printf("%ld.%06ld\n", end.tv_sec, end.tv_usec);
long t = (end.tv_sec - start.tv_sec)*1000000.0 + end.tv_usec - start.tv_usec;
return (double) t;
}
but i'm having a doubt: how does the vectorization works?
I mean, from what i've understood, the inner cicle is runned running multiple iterations together, but how can a single thread do this?
EDIT: code updated.

How can to free allocated memory

how can I free this malloc from function in main?
void insert(int data){
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp->data = data;
temp->next = head;
head = temp;
}
here is the main function, I did try to free the head but still the memory still increasing every time add some inputs
int main(){
head = NULL;
int n, data;
srand(time(NULL));
again:
printf("how many numbers to be entered in linked list? ");
scanf("%d", &n);
for(int i=0; i<n; i++){
data = rand() % 100;
insert(data);
print();
}
goto again;
free(head);
}

BinaryOperator doesn't work when comes to a=function(b,c)?

I want to identify the Expression like int a = function(b,c), so I wrote the code as followers:
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}
And than I used clang -Xclang -ast-dump -fsyntax-only cfunc_with_if.c to get the AST of the code:
From the result I found the AST Node type of int a = function(b,c) is BinaryOperator. In order to verify this, I use VisitStmt(Stmt *s) to print out all stmts' type.
bool VisitStmt(Stmt *s) {
if(isa<Stmt>(s)) {
Stmt *Statement = dyn_cast<Stmt>(s);
//Statement->dump();
std::string st(Statement->getStmtClassName());
st = st + "\n";
TheRewriter.InsertText(Statement->getLocStart(), st, true, true);
}
return true;
}
But the result is so weird. There is nothing printed out about the type of int a = function(b,c). and I'm so confused about the result. Is there some error in my code or something else?
There's no output at bar(x,m); either. Are there any errors when the tool compiles the code being analyzed? As written above, the code would fail to compile at x = function( sizeof(char)); since function has not been declared. Even when compilation has failed due to errors, the libtool tools can still run at least partially, with strange results.
Edit to add: what happens if you run the tool on this code?
void bar(float x, float y);
int function(int size);
void foo(int* a, int *b) {
int x;
int m;
int z;
int *p;
if (a[0] > 1) {
b[0] = 2;
z=10;
x = function( sizeof(char));
}
m = function( sizeof(char));
bar(x,m);
}
void bar(float x, float y);
int function(int size){
return size;
}

Finding bug in segment tree implementation

I was trying to solve this problem - link. Segment Tree with Lazy Propagation. But I dont know where I made mistake. Please help me find the bug.
I am new to Segment Tree with Lazy Propagation. But my code seems ok.
#include <bits/stdc++.h>
using namespace std;
const int MAXN = 100000;
struct info{ long long sum, prop; }; info tree[300010];
void update(int node, int l, int r, int i, int j, int val) {
if( i > r || j < l) return;
if(i <= l && j >= r) {
tree[node].sum += (r-l+1)*val;
tree[node].prop += val;
return;
} int left = node*2, right = left|1, mid = (l+r)/2;
update(left, l, mid, i,j,val);
update(right, mid+1, r, i,j,val);
tree[node].sum = tree[left].sum + tree[right].sum + (r-l+1)*tree[node].prop;
}
long long query(int node, int l, int r, int i, int j, long long carry = 0) {
if(i > r || j < l ) return 0;
if(i <= l && j >= r) return tree[node].sum + (r-l+1)*carry;
int mid = (l+r)/2, left = node*2, right = left|1;
long long ret = query(left, l, mid, i,j, carry + tree[node].prop);
ret += query(right,mid+1,r,i,j, carry + tree[node].prop);
return ret;
}
int main(int argc, char const *argv[]) {
#ifndef ONLINE_JUDGE
freopen("in", "r", stdin);
#endif
int t,co=0; scanf("%d", &t); while(t--) {
int n, q; scanf("%d %d", &n, &q);
for(int i=0; i<=3*n; i++) tree[i].sum = tree[i].prop = 0;
printf("Case %d:\n", ++co);
while(q--) {
int type,a,b,c; scanf("%d", &type); if(!type) {
scanf("%d %d %d", &a, &b, &c);
update(1, 0, n-1, a,b,c);
} else {
scanf("%d %d", &a, &b);
printf("%d\n", query(1, 0, n-1, a,b));
}
}
}
}
First of all this site is not for finding bug in code. Try https://codereview.stackexchange.com/ for this purpose.
Anyway In your code return type of function query() is long long but in main you are printing integer type. change printf("%d\n", query(1, 0, n-1, a,b)); to printf("%lld\n", query(1, 0, n-1, a,b)); and I hope you will get AC.

how to align in printf function

I want to make the printf function print from right to left because this program convert the value of number to binary and I want it to be printed in proper form for example if I convert 16 it is written like that 00001 but it must look like that 10000 so does anyone know how to do that thanks in advance
#include <stdio.h>
#include <stdlib.h>
int main()
{
int x,rem;
printf("please enter number: ");
scanf("%d",&x);
while (x !=0)
{
rem=x%2;
if (rem==0)
{
printf("0");
}
else
{
printf("1");
}
x = x/2;
rem = 0;
}
return 0;
}
Here it is:
void print_binary(int x)
{
int skip = 1;
unsigned int mask = 1 << 31;
while(mask > 0){
if(x & mask){
skip = 0;
printf("1");
}else{
if(!skip) printf("0");
}
mask >>= 1;
}
printf("\n");
}
This will print the binary number without trailing zeroes.
If you rather want the result to be stored in a string, you can use:
#include <string.h>
void int_to_binary(int x, char * buff) // buff size must be >= 32 !
{
buff[0] = '\0'; // ensure string ends with \0
unsigned int mask = 1 << 31;
for (; mask > 0; mask >>= 1)
{
strcat(buff, (x & mask) ? "1" : "0");
}
}
To check both codes, use:
int main(int argc, char* argv[])
{
int x;
printf("please enter number: ");
scanf("%d",&x);
char bin[32];
int_to_binary(x, bin);
printf("%s\n", bin);
print_binary(x);
}
What we do is using a mask, which in binary is one "1" beginning on the far left and moving one step right at each loop. The "&" is a bite-wise operator (I let you google it to know how it works). If you need more explanation, feel free to ask.
#include<stdio.h>
#include<stdlib.h>
int main()
{
int binary[20];
int q,i=0;
printf("Enter the decimal no\n");
scanf("%d",&q);
while(q > 0)
{
binary[i]=q%2;
i++;
q=q/2;
}
for(int j=i-1;j>=0;j--)
{
printf("%d",binary[j]);
}
return 0;
}

Resources