im trying to reverse a double linked list without using any extra pointer.I only have head pointer pointing to the first node of my double linked list.
here is my function to reverse the linked list:
int reverse(){
if(head==NULL || head->next==NULL){
return 0;
}
else{
head->prev=head->next;
head->next=NULL;
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
while(head->prev==NULL){
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
}
}}
if my linked list have data 1,2,3,4,5 and after running the reverse function ,when i'm trying to display the data i'm getting output: 2,1 (instead of 5,4,3,2,1)
there is a error in while statement while(head->prev==NULL)
the correct code is while(head->prev!=NULL)
so the correct code of the function is:
int reverse(){
if(head==NULL || head->next==NULL){
return 0;
}
else{
head->prev=head->next;
head->next=NULL;
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
while(head->prev!=NULL){
head->prev->prev=head->prev->next;
head->prev->next=head;
head=head->prev;
}
}}
Related
<DBDLinkedList.h>
...
typedef struct _dbDLinkedList
{
Node * head;
Node * tail;
Node * cur;
int numOfData;
} DBDLinkedList;
typedef DBDLinkedList List;
...
<mysourcecode.c>
int main(void)
{
...
List list;
int data;
ListInit(&list);
for(i=0; i<ID_LEN; i++)
LInsert(&list, new_id[i]);
solution(list, data);
...
}
int solution(List list, int data)
{
...
if(LFirst(&list, &data))
{
int i=1;
if(data==46)
LRemove(&list);
i++;
while(LNext(&list, &data))
{
if(i==numOfData)
if(data==46)
LRemove(&list);
i++;
}
}
...
}
I used dummy node doubly linked list.
When I complied this project, error occurred :
‘numOfData’ undeclared (first use in this function).
I'm not used to using Linked list.
How do I send the linked list as a function argument(for solution())?
from what I could see, the problem might not be in sending the linked list as a function argument. In "if(i==numOfData)", you are trying to access the numOfData, which is a part of a 'List' structure, so you need to access it through 'List' variable.... Perhaps replacing that line of code with something like "if(i == list.numOfData)" will do the trick
numOfData is not a variable. You most probably intended to access the current node's numOfData member.
But you really don't need to access that member directly. It seems likely (without seeing the code) that LNext will put that value in your data variable -- it is the reason why you pass &data as argument to LNext.
This brings us to another issue in your code. You call solution with data, but you never gave data a value. If the intention is to remove the node with value 46, then the main program should call solution as follows:
solution(list, 46);
And solution should be implemented as follows:
int solution(List list, int dataToRemove) {
if (!LFirst(&list, &data)) {
return 0; // to indicate that value to delete was not found
}
while (data != dataToRemove) {
if (!LNext(&list, &data)) {
return 0; // to indicate that value to delete was not found
}
}
LRemove(&list);
return 1; // to indicate success
}
I have tried everything to figure out what is wrong with my code. I tried to search for the problem. I tried to redo the code. I even tried to simulate the code several times on a piece of paper and It should work fine. But it's not. So far the code works when I input a root as 5, then add another node for example 8, then add another node 9, then add another node 6. But when I try to add another node for example 3. When I try to display all the nodes, the Node 6 and 9 are gone. But the node 3 was added. Any help would be appreciated.
bool addrec(nd *root,nd *tp,nd *tr,nd *data)//FIX ADDING
{
nd temp;
temp=malloc(sizeof(NODE));
temp->bkNumber=(*data)->bkNumber;
strcpy(temp->bkTitle, (*data)->bkTitle);
strcpy(temp->bkAuthor, (*data)->bkAuthor);
temp->bkCopyright=(*data)->bkCopyright;
temp->left=NULL;
temp->right=NULL;
bool added=false;
if(*root==NULL)
{
*root=temp;
added=true;
}
else
{
*tp=*root;
while(*tp!=NULL)
{
*tr=*tp;
if((*tp)->bkNumber>=(*data)->bkNumber)
*tp=(*tp)->left;
else
*tp=(*tp)->right;
}
if((*tr)->bkNumber>=(*data)->bkNumber)
{
(*tr)->left=temp;
}
else
{
(*tr)->right=temp;
}
added=true;
if((*tr)->right==NULL)//House Keeping To Make Sure the pointers of the new node is pointing to NULL
{
*tr=(*tr)->left;
(*tr)->left=NULL;
(*tr)->right=NULL;
}
else
{
*tr=(*tr)->right;
(*tr)->left=NULL;
(*tr)->right=NULL;
}
}
data=NULL;
temp=NULL;
*tr=NULL;
*tp=NULL;
return added;
}
I wrote a program to manage Bank accounts by the means of a doubly linked list, but I found a problem with the cancellation procedure.
void suppCompte (int numCpt) {
CompteBancaire *pnt = first;
if (first==NULL) {
printf("la liste vide !\n");
}else{
while (pnt != NULL && pnt->idf.numCompte != numCpt) {
pnt=pnt->next;
if (pnt==first) { // Remove the first node
first=pnt->next;
free(pnt);
}else if (pnt->next==NULL) { // Remove the last node
pnt->prc->next=NULL;
free(pnt);
}else{ // Remove a moddle node
pnt->prc->next=pnt->next; // <==== !!!!
pnt->next->prc=pnt->prc; // <==== !!!!
free(pnt);
}
}
}
}
I still have the same problem, even if I tried to do this method:
-(pnt->prc)->next=pnt->next;
The line after the while loop causes the problem, i.e. pnt=pnt->next should be after the if-else if . So if there is only 1 node, then pnt will be NULL, which causes problem in the else part. The modified code is :
void suppCompte (int numCpt)
{
CompteBancaire *pnt=first;
if (first==NULL)
printf("la liste vide !\n");
else
{
while (pnt!=NULL && pnt->idf.numCompte!=numCpt)
CompteBancaire *temp=pnt;
if (pnt==first) // Remove the first node
{ first=pnt->next;
}
else if (pnt->next==NULL) // Remove the last node
{ pnt->prc->next=NULL;
}
else // Remove a moddle node
{ pnt->prc->next=pnt->next; <==== !!!!
pnt->next->prc=pnt->prc; <==== !!!!
}
pnt=temp->next;
free(temp);
}
}
Check your pointers to make sure that they are not NULL. This can be done with two easy if loops. You always have to watch out for this sort of thing with doubly linked lists, and you have to think carefully about the order of your instructions.
Then, after you set the pointers to sort of "point around" the current node, set the pointers of the current node to NULL.
Also, consider using gdb. It is the Gnu DeBugger. If you compile with gcc, you can say gcc -g <files and other stuff> to compile with gdb debugging symbols. Then you can run the program in gdb, and inspect the values of variables, watch stuff evaluate, etc. You can probably find a lot of good material on this.
I'm coding a binary search tree and I'm having a little trouble finding a way to delete node effectively.
I have this code :
struct node* deleteNode(int i, struct node *N)
{
if (N==NULL)
{
return NULL;
}
else if (i<N->value)
{
N->size--;
N->lChild=deleteNode(i,N->lChild);
}
else if (i>N->value)
{
N->size--;
N->rChild=deleteNode(i,N->rChild);
}
else if (N->lChild==NULL)
{
return N->rChild;
}
else if (N->rChild==NULL)
{
return N->lChild;
}
else
{
N->size--;
N->value=findMin(N->rChild);
N->rChild=deleteNode(N->value,N->rChild);
}
return N;
}
And N is a node structure which have 5 fields : value, lChild, rChild, size, height.
In fact what I'm doing here is to make the tree not to point toward the node that I want to delete but when I'm trying to put something like :
else if (N->rChild==NULL)
{
free(N);
N=NULL;
return N->lChild;
}
Or every similar looking code, it doesn't work. Can someone point me in the right direction please?
Thank you.
First of all you're saying N=NULL and then calling N->lchild N is null and pointing to nothing so how do you expect to get the lchild value?
Since this is homework I won't give a direct answer but hints.
To delete the node, check if it has children, if it doesnt free it and remove references to it such as the parents child ptr.
If it has 1 child swap the ptr that points to the node you want to delete with the child and free the node. The same applies if you also have 2 children.
The code I have made is as follow...but it is not sorting the result at all.it is just printing the same as it is input. Please help me out
strcpy(newnode->stud_name,name);
strcpy(newnode->stud_intake,id);
strcpy(newnode->stud_branch,course);
newnode->level=slevel;
newnode->next=NULL;
if(list==NULL)
list=newnode;
else
{
if(slevel==list->level)
{
temp=list;
placefound=0;
while(temp!=NULL && slevel==temp->level)
{
if(name>temp->stud_name)
{
prev=temp;
temp=temp->next;
}//else
//placefound=1;
}
newnode->next=prev->next;
prev->next=newnode;
}
Without trying to debug your linked list code in detail, there's one very suspicious point:
if(name>temp->stud_name)
is probably wrong -- you can't compare C strings with ">". Look up how to use the "strcmp" function in the C standard library.