Question:
C programming problem?
?
2013-04-21 13:59:49 UTC
This program does a number of things on a linked list,but the problem i am having is in the copy function as i am trying to copy the linked list to another linked list recursively.Please examine the program and point out my mistake.Split function is not included because of the character limit.

#include
#include
#include
#include
#include
# define NULL 0
typedef struct node
{
int number;
struct node *next;
};
typedef struct node NODE;
NODE *start,*list,*rTail,*qTail,*temp,*temp1,*p,*q;
void Insert();
void Create();
void Display();
int Count();
void Update();
NODE* Copy();
void Split();
void Delete();

int count=0,pos,item,k;
int a[100],b[100],n;
void main()
{
int c;
char ch='y';
list=NULL;
clrscr();
do
{
printf("\n--------MENU---------");
printf("\n1.Create a link list");
printf("\n2.Display the link list");
printf("\n3.Count the number of elements");
printf("\n4.Update a node");
printf("\n5.Insert a node");
printf("\n6.Delete a node");
printf("\n7.Copy to another linked list");
printf("\n8.Split the linked list");
printf("\nEnter your choice");
scanf("%d",&c);
switch (c)
{
case 1:
Create(list);
Display(list);
break;
case 2:
Display(list);
break;
case 3:
count=count+Count(list);
Display(list);
printf("\nElement Number=%d\n",count);
break;
case 4:
Update(list);
Display(list);
break;
case 5:
Insert();
Display(list);
break;


case 6:
Delete(list);
Display(list);
break;
case 7:
Copy(list,start);
Display(list);
printf("\nThe Copy of the list\n");
Display(q);
break;

case 8:
Split();
Display(rTail);
printf("\n");
Display(qTail);
break;
default:
printf("\nYou have exited from the program");
exit(0);
}
fflush(stdin);
printf("\n Do you want to continue y/n");
scanf("%c",&ch);
}while(ch=='y' || ch=='Y');
}

void Create()
{
char ch='y';int n;
while(ch=='y' || ch=='Y')
{
printf("Enter an element");
scanf("%d",&n);
if(list==NULL)
{ p=(NODE *)malloc(sizeof(NODE));
p->next=NULL;
p->number=n;
list=p;
}
else
{ temp=list;
while(temp->next!=NULL)
{temp=temp->next;
}
p=(NODE *)malloc(sizeof(NODE));
p->number=n;
p->next= NULL;
temp->next=p;
} fflush(stdin);
printf("Do you want to add more data y/n ");
scanf("%c",&ch);
}
}
void Display(NODE *list)
{ NODE *temp;
temp=list;
while(temp!=NULL)
{
printf("%d\t",temp->number);
temp=temp->next;
}
}
int Count(NODE *list)
{ if(list==NULL)
return(0);
else
return (1+Count(list->next));
}

void Update(NODE *list)
{ printf("Enter the position of the node to be updated\n");
scanf("%d",&pos);
printf("Enter the element\n");
scanf("%d",&item);
temp=list;
for(k=0;k { if(temp->next==NULL)
{ printf("\n No such value in the list !!!");
return;
}
temp=temp->next;
}
temp->number=item;
}
NODE* Copy(NODE *list,NODE *q)
{ if(list!=NULL)
{
q=(NODE *)malloc(sizeof(NODE));
q->number=list->number;
q->next=NULL;

Copy(list->next,q->next) ;
}
}

void Delete(NODE *list)
{
printf("\nEnter the position of the node to be deleted");
scanf("%d",&pos);

temp=list;
if(pos==1)
list=list->next;
for(k=0;k temp=temp->next;

temp->next=(temp->next)->next;
}


void Insert()
{ int a;
printf("\nEnter the position of the node to be inserted");
scanf("%d",&pos);
printf("\nEnter the element to be inserted");
scanf("%d",&item);
temp=list;
p=(NODE *)malloc(sizeof(NODE));
p->number=item;
p->next=NULL;

if(pos==1)
{ p->next=temp;
list=p;
}
else{

for(a=0;a temp=temp->next;


p->next=(temp->next)->next;
temp->next=p;
temp=temp->next;
}
}
Four answers:
Jackie
2013-04-21 14:07:42 UTC
Hi Kunal,



Please post this code to http://pastebin.com/ so it can show up properly formatted. It's too much work for us to re-format this code just so we can read it.



Thanks,

Jackie
RAVINDRA LOHANI
2013-04-22 06:31:38 UTC
Line 18: error: conio.h: No such file or directory

Line 20: error: process.h: No such file or directory

Line 0: warning: "NULL" redefined

In file included from /usr/include/stdlib.h:33,

from /usr/include/bits/string2.h:1211,

from /usr/include/string.h:375,

from t.c:6:

Line 0: warning: this is the location of the previous definition

Line 11: warning: useless storage class specifier in empty declaration

Line 13: error: stray '\342' in program

Line 13: error: stray '\200' in program

Line 13: error: stray '\246' in program

Line 14: error: expected identifier or '(' before 'void'

In function 'main':

Line 76: error: 'q' undeclared (first use in this function)

Line 76: error: (Each undeclared identifier is reported only once

Line 76: error: for each function it appears in.)

Line 87: warning: incompatible implicit declaration of built-in function 'exit'

Line 26: warning: return type of 'main' is not 'int'

In function 'Create':

Line 103: error: 'p' undeclared (first use in this function)

t.c: At top level:

Line 180: warning: conflicting types for 'Insert'

Line 63: warning: previous implicit declaration of 'Insert' was here

In function 'Insert':

Line 186: error: 'p' undeclared (first use in this function)
?
2013-04-23 01:15:00 UTC
Splits the array is one the most simple program in C language.I read your whole program but I found so many mistakes in it.



So you can check my program:-Q)Write a program to create array and split the array into two parts i.e. even and odd.



http://allcomputertopics.blogspot.in/2013/03/program-to-split-array-into-another.html
?
2016-10-28 12:21:06 UTC
write a module to opposite a given string "i'm puzzled" would desire to develop into "desufnoc ma i" Then cut up the string on areas and pass each and each word back to a similar module. "desufnoc ma i" would desire to develop into "puzzled am i" Thats one way that would desire to artwork


This content was originally posted on Y! Answers, a Q&A website that shut down in 2021.
Loading...