C 向链表中具有字符数组的节点分配内存
这是一个简单的c语言单链表程序C 向链表中具有字符数组的节点分配内存,c,linked-list,C,Linked List,这是一个简单的c语言单链表程序 struct node { int id; char name[20]; int sem; struct node *link; }; typedef struct node* Node; Node getnode() { Node temp=(Node)(malloc(sizeof(Node))); if(temp==NULL) printf("\n Out of memory");
struct node
{
int id;
char name[20];
int sem;
struct node *link;
};
typedef struct node* Node;
Node getnode()
{
Node temp=(Node)(malloc(sizeof(Node)));
if(temp==NULL)
printf("\n Out of memory");
return temp;
}
Node ins_pos(Node first)
{
Node temp=getnode();
printf("\n Enter id ");
scanf("%d",&temp->id);
printf("\n Enter name ");
scanf("%s",temp->name);
printf("\n Enter semester ");
scanf("%d",&temp->sem);
if(first == NULL)
{
temp->link=NULL;
return temp;
}
else
{
int pos,i;
printf("\n Enter position: ");
scanf("%d",&pos);
if(pos == 1)
{
temp->link=first;
return temp;
}
else
{
Node prev=NULL,cur=first;
for(i=1; i<pos; i++)
{
if(cur==NULL)
break;
prev=cur;
cur=cur->link;
}
if(cur==NULL && i < pos)
printf("\n Position invalid");
else
{
prev->link=temp;
temp->link=cur;
}
return first;
}
}
}
Node del(Node first)
{
if(first==NULL)
printf("\n List is Empty ");
else
{
Node temp=first;
printf("\n ID: %d was deleted",temp->id);
first=first->link;
free(temp);
}
return first;
}
void disply(Node first)
{
if(first==NULL)
printf("\n List is empty");
else
{
Node cur=first;
while(cur!=NULL)
{
printf("\n ID : ");
printf("%d",cur->id);
printf("\n Name : ");
printf("%s",cur->name);
printf("\n Semester : ");
printf("%d",cur->sem);
printf("\n\n\n");
cur=cur->link;
}
}
}
int main()
{
Node first=NULL;
int opt;
do
{
printf("\n QUEUE MENU\n 1.Insert at position \n 2.delete front\n 3.display\n 4.Exit \n\n Enter your choice : ");
scanf("%d",&opt);
switch(opt)
{
case 1 :first = ins_pos(first);
break;
case 2 :first = del(first);
break;
case 3 :disply(first);
break;
}
}while(opt!=4);
return 0;
}
struct节点
{
int-id;
字符名[20];
int-sem;
结构节点*链接;
};
类型定义结构节点*节点;
节点getnode()
{
节点温度=(节点)(malloc(sizeof(节点));
if(temp==NULL)
printf(“\n内存不足”);
返回温度;
}
节点插入位置(节点第一)
{
Node temp=getnode();
printf(“\n输入id”);
scanf(“%d”,&temp->id);
printf(“\n输入名称”);
扫描(“%s”,临时->名称);
printf(“\n输入学期”);
scanf(“%d”,&temp->sem);
if(first==NULL)
{
temp->link=NULL;
返回温度;
}
其他的
{
int pos,i;
printf(“\n输入位置:”);
scanf(“%d”和“&pos”);
如果(位置==1)
{
临时->链接=第一;
返回温度;
}
其他的
{
节点prev=NULL,cur=first;
对于(i=1;ilink;
}
如果(cur==NULL&&i链接=温度;
温度->链接=电流;
}
先返回;
}
}
}
节点删除(节点优先)
{
if(first==NULL)
printf(“\n列表为空”);
其他的
{
节点温度=第一;
printf(“\n ID:%d已删除”,临时->ID);
第一个=第一个->链接;
免费(临时);
}
先返回;
}
void display(节点优先)
{
if(first==NULL)
printf(“\n列表为空”);
其他的
{
节点cur=第一;
while(cur!=NULL)
{
printf(“\n ID:”);
printf(“%d”,cur->id);
printf(“\n名称:”);
printf(“%s”,cur->name);
printf(“\n学期:”);
printf(“%d”,cur->sem);
printf(“\n\n\n”);
cur=cur->link;
}
}
}
int main()
{
Node first=NULL;
int-opt;
做
{
printf(“\n队列菜单\n 1.在位置插入\n 2.删除前面\n 3.显示\n 4.退出\n\n输入您的选择:”);
scanf(“%d”和&opt);
开关(opt)
{
情况1:first=ins_pos(first);
打破
情况2:第一个=删除(第一个);
打破
案例3:dispy(第一);
打破
}
}while(opt!=4);
返回0;
}
插入新节点时,代码块在malloc语句处崩溃。我怎么知道?嗯,它在请求“输入ID”之前崩溃。那么,我是否做错了什么
这里的另一点是,它只适用于节点中的整数字段,这里的问题可能是字符数组。您需要使用
node temp=(node)(malloc(sizeof(*temp));
这里有一些错误
malloc( sizeof( struct node ) );
否则,分配的内存太少
是否为malloc定义包含
stdlib.h
,这会导致此问题(无定义,默认为int)。在此函数中Node getnode()
-
使用上述malloc
分配的内存等于Node
的大小,该节点属于struct-pointer类型,内存不足。因此,您会遇到分段错误
与其这样,不如这样写-
Node temp=malloc(sizeof(*temp)); //also there is no need of cast
这将分配与
temp
指向的类型大小相等的内存,即与结构大小相等的内存。这是适当的。对不起,疏忽。更正。我注意到这个答案没有解释为什么会解决问题。@immibis我已经添加了解释。如果您看到任何问题,请随时指出。谢谢 !! :-)
Node temp=malloc(sizeof(*temp)); //also there is no need of cast