Click here to Skip to main content
16,004,406 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
#include <stdio.h>
#include <stdlib.h>

typedef struct _cvor
{
	int vrednost;
	struct _cvor *sledeci;
}cvor;

cvor* napraviCvor(int x, cvor* naredni)
{
cvor *a=(cvor*)malloc(sizeof(cvor));
	a->vrednost=x;
	a->sledeci=naredni;
	return a;
}

void dodajCvor(int x, cvor** lista)
{
	if((*lista)==NULL)
	{
		(*lista)=napraviCvor(x,NULL);
		return;
	}
cvor* b=napraviCvor(x,NULL);
cvor* pocetak;
pocetak=(*lista);
while(pocetak!=NULL && pocetak->sledeci!=NULL)
pocetak=pocetak->sledeci;
pocetak->sledeci=b;

	return;
}
int brojEl(cvor *lista)
{
    int brojac=0;
    if(lista==NULL)
        return brojac;
    while(lista->sledeci!=NULL)
    {
        brojac++;
        lista=lista->sledeci;
    }
    return (brojac+1);
}
void ispis(cvor *lista)
{
	if(lista==NULL)
	return;
	printf("%d ",lista->vrednost);
	ispis(lista->sledeci);
}
cvor* pronadjiPocetak(cvor *lista)
{
    if(lista==NULL)
    return NULL;

    return lista;

}
cvor* pronadjiKraj(cvor *lista)
{
 if(lista==NULL)
 return NULL;
    while(lista->sledeci!=NULL)
        lista=lista->sledeci;
    return lista;
}
void izmeniListu(cvor* lista1,cvor* lista2)
{
    cvor *pocetak;
    cvor *kraj;
    pocetak=pronadjiPocetak(lista1);
    kraj=pronadjiKraj(lista1);
    printf("\n Prvi element: %d",pocetak->vrednost);
	printf("\n Poslednji element: %d",kraj->vrednost);

   while(brojEl(lista1)){
   if(brojEl(lista1)==1)
   {
    dodajCvor(pocetak->vrednost,&lista2);
    return;
   }
	printf("\n%d",lista2->vrednost);
  dodajCvor(pocetak->vrednost,&lista2);
  dodajCvor(kraj->vrednost,&lista2);
  cvor* tekuci;
  tekuci=pocetak;
  while(tekuci->sledeci->sledeci!=NULL)
  tekuci=tekuci->sledeci;
 kraj=tekuci;
free(tekuci->sledeci);
cvor *temp;
temp=pocetak;
 pocetak->sledeci=pocetak->sledeci->sledeci;
  pocetak=pocetak->sledeci;
free(temp);
   
   }

}
int main()
{
	int broj;
	cvor *lista=NULL;
	cvor* lista2=NULL;
	int n;
	int brojac=0;
	int k;
	printf("\n Unesite broj elemenata liste:");
	scanf("%d",&n);
	printf("\n Unos clanova liste:");
	while(brojac<n)
	{
		scanf("%d",&broj);
		dodajCvor(broj,&lista);
		brojac++;
	}

	printf("\n Izgled liste: ");
	ispis(lista);
	printf("\n Broj elemenata liste: %d",brojEl(lista));	
    izmeniListu(lista,lista2);
    printf("\n Izgled izmenjene liste je:");
    ispis(lista2);
	return 0;
}


What I have tried:

I know it's problem with function izmeniListu(), but I don't know how to solve this. Without that programme works fine. That function is made to reorganize singly linked list.
Posted
Updated 15-Mar-22 5:23am
Comments
jeron1 15-Mar-22 11:21am    
That's 0xC0000005 Access Violation, probably an invalid pointer. Can you step through that function, and see what line it fails on? (assuming it fails in that function)

1 solution

OK ... 3221225477 in hex is 0xC0000005, and Error code 0xC0000005 is "access violation" - which is a broad term meaning "you tried to access memory that isn't yours".
It's normally a sign that a pointer value is invalid, but I can't make head or tail of your code - it's badly indented so it's hard to read, none of the names make any sense to me, I have no clue how to use it, and I have no idea what the heck it is supposed to do anyway!

So, it's going to be up to you.
Fortunately, you have a tool available to you which will help you find out what is going on: the debugger. How you use it depends on your compiler system, but a quick Google for the name of your IDE and "debugger" should give you the info you need.

Put a breakpoint on the first line in the function, and run your code through the debugger. Then look at your code, and at your data and work out what should happen manually. Then single step each line checking that what you expected to happen is exactly what did. When it isn't, that's when you have a problem, and you can back-track (or run it again and look more closely) to find out why.

Sorry, but we can't do that for you - time for you to learn a new (and very, very useful) skill: debugging!
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900