#include<stdio.h>
#include<stdlib.h> //malloc defined
struct node
{
int data;
struct node *next;
};
add() //add function
{
int value;
struct node *n;
n=(struct node*)malloc(sizeof(struct node)); //mem allocation
printf("enter the value to add\n");
scanf("%d",&value);
n->data=value;
n->next=NULL;
// n=n->next;
// n->next=NULL;
}
delete() //delete function
{
// n=n->next;
struct node *n; //declaration
printf("the node deleted is %d",n->data);
free(n);
}
display() //display function
{
struct node *n;
while(n!=NULL)
{
printf("%d",n->data);
n=n->next;
}
}
int main()
{
int ch;
while(1)
{
printf("do you want to add node press 1\n");
printf("do you want to delete node press 2\n");
printf("do you want to display node press 3\n");
printf("do you want to exit press 4\n");
scanf("%d",&ch);
switch(ch)
{
case 1:add();
break;
case 2:delete();
break;
case 3:display();
break;
case 4:exit(0);
default: printf("wrong choice!!!\n");
}
}
return 0;
getch();
}
please help me this program is not working properly.
没有显示值。这个程序是我试图在c上运行的单链表的一个例子。
答案 0 :(得分:0)
printf("%d",n->data);
未打印,因为:
struct node *n; // n is not determined.
while(n != NULL) // undefined behaviour
n
与n
函数中的其他add()
不同。您从未将结构传递给其他函数,因此它不会执行您希望它执行的操作。
答案 1 :(得分:0)
答案 2 :(得分:0)
cSplit function