Click here to Skip to main content
15,881,938 members
Please Sign up or sign in to vote.
1.00/5 (2 votes)
See more:
C++
//Delete Single Linklist
#include <iostream>
using namespace std;

struct TheCell
{
  int dat;
  struct TheCell *berikut;
};

struct TheCell *isiCell(int dat, struct TheCell *cellBaru, struct TheCell *kepala){
  if (kepala == NULL){

    cellBaru = (struct TheCell *) malloc(sizeof(struct TheCell));
    cellBaru->dat=dat;
    
    cellBaru->berikut = NULL;
    return(cellBaru);
  }
  else{
    cellBaru->berikut=(struct TheCell *) malloc(sizeof(struct TheCell));
    cellBaru = cellBaru->berikut;
    
    cellBaru->dat=dat;
    
    cellBaru->berikut = NULL;
    return(cellBaru);
  }
}

void hapusCell(struct TheCell *cellTujuan, struct TheCell *kepala){
  struct TheCell *cellCheck;
  cellCheck = kepala;
  if(cellTujuan->berikut != NULL){
    while(cellCheck->berikut!=cellTujuan){
        cellCheck = cellCheck->berikut;
    };
      
    cellCheck->berikut = cellTujuan->berikut;
    free(cellTujuan);
  }
  else{
    while(cellCheck->berikut->berikut!=NULL){
        cellCheck = cellCheck->berikut;
    };
    free(cellCheck->berikut);
    cellCheck->berikut = NULL;
  }  
}

void cetak(struct TheCell *cellTujuan, struct TheCell *kepala){
  if (kepala!=NULL) 
  {
    cout<<"cell: {Alamat} [ Data | Cell berikut]"<<endl;
 celltujuan="kepala;
" do
="" {
="" cout<<"cell:="" {"<<celltujuan<<"}="" ["<<celltujuan-="">dat<<" | "<<celltujuan->berikut<<"]"<<endl;
 celltujuan="cellTujuan-">berikut;
    }while (cellTujuan != NULL);
    cout<<endl<<endl;
 }="" 
}

int="" main(){
="" int="" i;="" 
="" nodeke;
="" struct="" thecell="" *kepala="NULL;
" *ptrcell="NULL;
" for="" (i="1;i<=7;i++){
" if="" (kepala="=" null){
="" kepala="isiCell(i*10," null,="" null);;
="" ptrcell="kepala;
" }
="" else
="" {
="" ptrcell,="" kepala);
="" cetak(ptrcell,="" cout<<"hapus="" node="" ke:="" ";
="" cin="">>nodeke;
  cout<<endl;
 
="" ptrcell="kepala;
" if="" (nodeke="">1){
    for(i=1;i<nodeke;i++){
 ptrcell="ptrCell-">berikut;
    }
    hapusCell(ptrCell, kepala);
  }
  else{
	kepala=kepala->berikut;
  	free(ptrCell);
  }
  cetak(ptrCell, kepala);
  
  return 0;
}


What I have tried:

I tried change the "for" and parameter "int"
Posted
Updated 23-Feb-23 8:07am
v2
Comments
OriginalGriff 23-Feb-23 2:07am    
This is not a good question - we cannot work out from that little what you are trying to do.
Remember that we can't see your screen, access your HDD, or read your mind - we only get exactly what you type to work with - we get no other context for your project.
Imagine this: you go for a drive in the country, but you have a problem with the car. You call the garage, say "it broke" and turn off your phone. How long will you be waiting before the garage arrives with the right bits and tools to fix the car given they don't know what make or model it is, who you are, what happened when it all went wrong, or even where you are?

That's what you've done here. So stop typing as little as possible and try explaining things to people who have no way to access your project!

Use the "Improve question" widget to edit your question and provide better information. And repaste the code - it doesn't look right even when I engage the formatter!
CPallini 23-Feb-23 2:07am    
Could you, please, elaborate?

Your question is a bit unclear and your code isnt of good quality, so try with starting some Learn C++ tutorial or some video tutorial.

some tips:
- use complete english wording so any coder can understand it
- use functions and classes with understandable names and install some IDE like Visual Studio.
- write tests to demonstrate the usage, accomplish clear goals and verify the functionality
 
Share this answer
 
The uploaded source code contains a lot of illegal special characters as well as meaningless places.
C++
= "" int = "" i; = ""
= "" nodeke;
= "" struct = "" thecell = "" * kepala = "NULL;
" *ptrcell="NULL;

The variable nodeke also appears to be an int, though the declaration goes wrong here. The same is true for the pointer ptrcell.

TheCell is often written differentlyand cellTujuan is not case sensitive. The statement (kepala = null) is invalid. Either one uses NULL or the C++ designation nullptr for a pointer. Bad style would be to mix the two all the time.

The following approach is completely confused, since here among other things apparently cout is used as a parameter of a function.

C++
= "" else
= "" {
	= "" ptrcell, ="" kepala);
	= "" cetak(ptrcell, ="" cout << "hapus="" node="" ke:="" ";
	= "" cin = "" >> nodeke;
	cout << endl;

The program has so many meaningless parts that it hardly makes sense to answer a question on this basis. At least it should be possible to translate a source code without errors.

Instead of using malloc() to get memory for structures, it is better to define a class and use the new() function when needed.
C++
(struct TheCell*)malloc(sizeof(struct TheCell))
 
Share this answer
 
v3

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