How can I fix this linked list output? - c

#include <stdio.h>
#include <stdlib.h>
struct node{
int sayi;
struct node* sonraki;
};
struct node* baslangic=NULL;
struct node* olustur(int sayi){
struct node* yenidugum=(struct node*)malloc(sizeof(struct node));
yenidugum->sayi=sayi;
yenidugum->sonraki=NULL;
return yenidugum;
}
void sonaekle(int sayi){
struct node* sonaeklenecek=olustur(sayi);
if(baslangic==NULL){
baslangic=sonaekle;
}
else
{
struct node* temp=baslangic;
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
}
}
void yazdir(){
struct node* temp=baslangic;
while(temp!=NULL){
printf("%d =>",temp->sayi);
temp=temp->sonraki;
}
}
int main()
{
int secim, sayi;
while(1){
printf("1-Sona eleman ekle.....\n");
printf("Yapmak istediginiz secimi yapin...\n");
scanf("%d",&secim);
switch(secim){
case 1:
printf("Hangi elemani ekleyeceksiniz..\n");
scanf("%d",&sayi);
sonaekle(sayi);
yazdir();
break;
}
}
return 0;
}
I am trying to do make linked list but when I run this code it gives output like this:-443987883 =>. What is my mistake. I can't find it. Thank you for your answers and giving your time.

There are typos.
Instead of
baslangic=sonaekle;
where you are assigning the function pointer of the type void ( * )( int ) to the pointer of the type struct node * you need to write
baslangic=sonaeklenecek;
Instead of this while loop
while(temp->sonraki!=NULL){
temp=temp->sonraki;
temp->sonraki=sonaekle;
}
you need to write
while(temp->sonraki!=NULL){
temp=temp->sonraki;
}
temp->sonraki = sonaeklenecek;

Related

How can I restart a double linked list?

I'm coding a program to multiply two polynomials. I need some advice as to how I can restart the double linked list in the nested while loop in the polyProduct function. I mean, at this point I need to go back to the first position of the list. This is the code I have so far:
#include "DLListInterface.h"
#include <stdio.h>
#include <stdlib.h>
int numRead(DLnode*,int);
void printList(DLnode*);
DLnode *polyProduct(DLnode*,DLnode*);
int main()
{
DLnode *number1;
DLnode *number2;
DLnode *result;
int readval;
int numbterms;
int i;
scanf("%d",&numbterms);
scanf("%d",&readval);
/* initial DLnode of number1*/
number1=(DLnode*)malloc(sizeof(DLnode));
number1->value=readval;
number1->next=NULL;
number1->prev=NULL;
readval=numRead(number1,numbterms);
scanf("%d",&numbterms);
scanf("%d",&readval);
number2=(DLnode*)malloc(sizeof(DLnode));
number2->value=readval;
number2->next=NULL;
number2->prev=NULL;
readval=numRead(number2,numbterms);
printf("\n");
printList(number1);
printList(number2);
result=polyProduct(number1,number2);
printList(result);
getch();
return 0;
}
int numRead(DLnode*number,int numbterms)
{
int readval,i;
DLnode *anothernode;
for(i=0;i<(numbterms*2)-1;i++)
{
scanf("%d",&readval);
anothernode=(DLnode*)malloc(sizeof(DLnode));
anothernode->value=readval;
anothernode->next=NULL;
anothernode->prev=Dlast(number);
Dlast(number)->next=anothernode;
}
return readval;
}
DLnode *polyProduct(DLnode*poly1,DLnode*poly2)
{
DLnode *result;
DLnode *newnode;
DLnode *first;
first=poly2;
newnode=(DLnode*)malloc(sizeof(DLnode));
newnode->value=(poly1->value)*(poly2->value);
newnode->next=NULL;
newnode->prev=NULL;
result=newnode;
poly1=poly1->next;
poly2=poly2->next;
newnode=(DLnode*)malloc(sizeof(DLnode));
newnode->value=poly1->value+poly2->value;
newnode->next=NULL;
newnode->prev=Dlast(result);
Dlast(result)->next=newnode;
poly1=poly1->prev;
poly2=poly2->next;
while(poly1!=NULL)
{
while(poly2!=NULL)
{
newnode=(DLnode*)malloc(sizeof(DLnode));
newnode->value=(poly1->value)*(poly2->value);
newnode->next=NULL;
newnode->prev=Dlast(result);
Dlast(result)->next=newnode;
poly1=poly1->next;
poly2=poly2->next;
newnode=(DLnode*)malloc(sizeof(DLnode));
newnode->value=poly1->value+poly2->value;
newnode->next=NULL;
newnode->prev=Dlast(result);
Dlast(result)->next=newnode;
poly1=poly1->prev;
poly2=poly2->next;
}
poly1=poly1->next;
poly2=first; //restart poly 2
}
return result;
}
void printList(DLnode*number)
{
while (number!=NULL)
{
printf("%d ",number->value);
number=number->next;
}
printf("\n");
return;
}
//Declaration of DLnode in DLList.h file
typedef struct Dcontainer
{
int value;
struct Dcontainer *next;
struct Dcontainer *prev;
} DLnode;
One way of going to the start of the list is to manipulate the pointer to move to the start of the list
void seekstart(DLNode* number)
{
while(number->prev != null)
{
number = number->prev;
}
}
The other option is use temporary nodes to store the Starting node of the list before processing.
DLnode *temp_num1 = number1;
DLnode *temp_num2 = number2;
result=polyProduct(number1,number2);
Or You keep a pointer to the root in the structure of the node itself. Although this might not be an efficient solution. It is quite helpful if you need to access the starting of the list quite frequently
//Declaration of DLnode in DLList.h file
typedef struct Dcontainer
{
int value;
struct Dcontainer *next;
struct Dcontainer *prev;
struct Dcontainer *root; //root is the starting of the list
} DLnode;

Value of struct member changes after printing C

I got this code and a strange behaviour while printing the id member variable of node.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct node
{
int id;
int visited;
// struct node *neighbors_[];
};
struct graph
{
struct node nodes[26];
int adjMat[26][26];
};
struct stack_item
{
struct node node;
struct stack_item *next_;
};
struct myStack
{
struct stack_item *anfang_;
};
void initGraph(struct graph *graph_);
void push(struct myStack *stack_, struct node node);
int main()
{
struct graph graph;
struct myStack stack;
char ausgabe[26]="";
initGraph(&graph);
//READ DATA
char line[200];
int firstTime=1,first;
first=0;
push(&stack,graph.nodes[first]);
printf("ID %i\n",stack.anfang_->node.id);
printf("ID %i\n",stack.anfang_->node.id);
//FINISHED DATA READING
//CALL DFS
//dfs(graph,stack,ausgabe);
}
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
stack_->anfang_=&item;
}
void initGraph(struct graph *graph_)
{
int i,j;
for(i=0; i<26; i++)
{
struct node node= {i,0};
graph_->nodes[i]=node;
for(j=0; j<26; j++)
{
graph_->adjMat[i][j]=0;
}
}
}
If i execute this, the first print command leads to 'ID 0',the second to 'ID 1980796117'. How can this value change by printing it? Could please anyone help me, i've got really no idea!
void push(struct myStack *stack_, struct node node)
{
struct stack_item item;
item.node=node;
item.next_=stack_->anfang_;
/* BAD! */
stack_->anfang_=&item;
}
item is a local variable which, when the push function returns, goes out of scope. Any existing pointers which refer to this object are now invalid, and dereferencing it results in undefined behavior.
You will need to dynamically allocate item (i.e., malloc) if you need it to persist once the function has returned.

Unexpected output in a linked list

Sometime back I asked a question about linked list and got nice replies...Now I've written a new code using the suggestions but I've run into an error. The code is:
#include<stdio.h>
#include<stdlib.h>
typedef struct node
{
int data;
struct node *next;
}node;
node *mknode()
{
return malloc(sizeof(node));
}
void create(node* h, int num)
{
int i;
node *temp=h;
for(i=0;i<num;i++)
{
temp->data=i;
if(i==(num-1))
temp->next=NULL;
else
temp->next=mknode();
temp=temp->next;
}
}
node* add_end(node *h,int num)
{
node *temp;
if(h==NULL)
{
h=mknode();
temp=h;
create(h,num);
}
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
return temp;
}
void display(node *h)
{
node *temp=h;
while(temp!=NULL)
{
printf("%d->",temp->data);
temp=temp->next;
}
}
int main()
{
node *head=NULL;
int num;
scanf("%d",&num);
head=add_end(head,num);
head=add_end(head,num);
display(head);
//printf("%d",list_len(head));
free(head);
return 0;
}
Now since I've called add_end twice for an input of 3 the output should be
0->1->2->0->1->2->
But instead I'm getting
0->1->2->
I've checked this much that the FOR loop inside create function is running 2n times for an input of n.
So the problem is that display function encounters a NULL but I can't figure out where in the code is it happening.
All help appreciated.
Thanking in advance
-user1614886
In add_end() you do not link the nodes correctly.
[blah blah]
else
{
temp=h;
while(h!=NULL){
h=h->next;}
h=mknode();
create(h,num);
}
You advance h until it is NULL but you should only move until h->next == NULL and append your next list there.

How to allocate memory?

I am trying to create a linked list in my program and I am not able to allocate memory to the structure pointer using malloc(). How do I allocate memory to variables in GCC? The sample program is given below. How to make it work in gcc?
#include<stdio.h>
#include <alloc.h>
struct node
{
int data;
struct node * link;
};
void insert (struct node *p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(p==NULL)
{
p=temp;
}
else{
while(p->link!=NULL)
p=p->link;
p->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(p,7);
insert(p,9);
disp(p);
}
The error I'm encountering is:
Line 18: error: alloc.h: No such file or directory
In function 'insert':
Line 13: warning: incompatible implicit declaration of built-in function 'malloc'
malloc is in <stdlib.h>. Include that.
Reading the man page for that function would have given you that information. It's not compiler-dependent.
malloc is declared in <stdlib.h>, so that's what you want to #include.
The definition of malloc is in the stdlib.h file:
#include <stdlib.h>
instead of alloc.h.
Like the others say: your error occurs because you have to include stdlib.h instead of alloc.h
To get your list printed, you have to modify p in insert. Currently, you're passing NULL every time you call insert. Change your code that way (pass a pointer-to-pointer to insert):
#include <stdio.h>
#include <stdlib.h>
struct node
{
int data;
struct node * link;
};
/* note **p instead of *p */
void insert (struct node **p, int d)
{
struct node *temp;
temp = malloc(sizeof(struct node));
temp->data=d;
temp->link=NULL;
if(*p==NULL)
{
*p=temp;
}
else{
while((*p)->link!=NULL)
*p=(*p)->link;
(*p)->link=temp;
}
}
void disp(struct node *p)
{
while(p!=NULL)
{
printf("%d\n",p->data);
p=p->link;
}
}
int main()
{
struct node *p;
p=NULL;
insert(&p,7);
insert(&p,9);
disp(p);
}
and it will print
7
9

creating a queue of pointers in c

i have a dynamic number of pointers all having the same size. i need to store all the addresses of my pointers in some place like a link List in order to fetch them later on.
my question is what structs should i use. is the following correct:
struct Node{
int *k;
Node*Next;
}
struct LS{
Node*first,*last;
void push(Node*n);
Node* GetFirst();
Node* GetLast();
}
the LS is the linked list that stores Nodes. and a Node is a struct that holds the address of my pointer and a pointer to the next Node.
am i using int *k to store the address of my pointer correctly? should i continue with this implementation or is there any easier way to do this?
this sample code may help you start...
#include <stdio.h>
struct Node{
int *k;
Node *Next;
}* Temp;
struct LS
{
Node *first,*last;
void push(Node *MyNode)
{
MyNode->Next=NULL;
if(empty())
{
first=MyNode;
last=MyNode;
}
else
{
last->Next = MyNode;
last=MyNode;
}
}
Node* front()
{
return first;
}
void pop()
{
free(first->k);
first=first->Next;
}
bool empty()
{
if(first==NULL) return true;
return false;
}
};
int N=10;
int main()
{
LS Q;Q.first=NULL;
for(int i=0;i<3;i++)
{
Node *NewNode= (Node*)malloc(sizeof(Node));
NewNode->k = (int*)malloc(sizeof(int)*N);
for(int k=0;k<N;k++) NewNode->k[k]=i;
Q.push(NewNode);
}
while(!Q.empty())
{
Temp=Q.front();
for(int i=0;i<N;i++) printf("%d ",Temp->k[i]);
printf("\n");
Q.pop();
}
return 1;
}
Yes, your Node struct is correct.
As to whether there is an easier way it depends. If there is a maximum number of pointers that you will need then an array of pointers would be easier. If you can do it in C++ then an STL vector (can use it like an array, but underneath the hood it can grow dynamically as needed) is easier. If you have to do it in C and it has to be dynamic, though, then no, there is not an easier way.
WDM.H (microsoft header) has a bunch of linked list stuff to look at ( http://msdn.microsoft.com/en-us/library/ff547799(VS.85).aspx ) , I've cut and pasted from that, and added a very simple example.
typedef struct _LIST_ENTRY {
struct _LIST_ENTRY *Flink;
struct _LIST_ENTRY *Blink;
} LIST_ENTRY, *PLIST_ENTRY;
typedef struct _MY_THING
{
LIST_ENTRY ListEntry;
ULONG randomdata1;
ULONG randomdata2;
ULONG randomdata3;
ULONG randomdata4;
} MY_THING, *PMY_THING;
#define CONTAINING_RECORD(address, type, field) ((type *)( \
(PCHAR)(address) - \
(ULONG_PTR)(&((type *)0)->field)))
VOID
InsertHeadList(
IN PLIST_ENTRY ListHead,
IN PLIST_ENTRY Entry
)
{
PLIST_ENTRY Flink;
Flink = ListHead->Flink;
Entry->Flink = Flink;
Entry->Blink = ListHead;
Flink->Blink = Entry;
ListHead->Flink = Entry;
}
VOID
InitializeListHead(
IN PLIST_ENTRY ListHead
)
{
ListHead->Flink = ListHead->Blink = ListHead;
}
PLIST_ENTRY
RemoveHeadList(
IN PLIST_ENTRY ListHead
)
{
PLIST_ENTRY Flink;
PLIST_ENTRY Entry;
Entry = ListHead->Flink;
Flink = Entry->Flink;
ListHead->Flink = Flink;
Flink->Blink = ListHead;
return Entry;
}
void main()
{
LIST_ENTRY HeadOfMyList;
MY_THING Thing;
InitializeListHead(&Head);
// example of add thing to list.
InsertHeadList(&HeadOfMyList, &Thing.ListEntry);
// example of removing thing from the list
PLIST_ENTRY listEntry = RemoveHeadList(&HeadOfMyList);
PMY_THING pThing = (PMY_THING) CONTAINING_RECORD(listEntry, MY_THING, ListEntry);
}

Resources