[英]Doubly Linked List with strings
I'm trying to build a doubly linked list in C by inputting strings and I want to be clear if i'm right. 我正在尝试通过输入字符串在C中建立一个双向链接列表,如果我是对的,我想弄清楚。 It's hard for me to explain so I'll just post what I have.
我很难解释,所以我只发布我所拥有的。 I'm new to C so please correct me and I hope i'm right and please let me know if it's best to post my whole code.
我是C语言的新手,所以请纠正我,我希望我是对的,请让我知道最好发布整个代码。
Okay so I have my struck which looks like this: 好吧,我的表情看起来像这样:
struct node
{
char data[100];
struct node *previous; // Points to the previous node
struct node *next; // Points out to the next node
}*head, *last;
And this is a function to insert at the beginning: 这是一个要插入的函数:
void insert_beginning(char words[100])
{
struct node *var, *temp;
var=(struct node *)malloc(sizeof(struct node));
var->data[100]=words[100];
if (head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
and this is my main: 这是我的主要:
int main()
{
char words[100];
int i;
head=NULL;
printf("Select the choice of operation on link list");
printf("\n1.) insert at begining\n2.) insert at at\n3.) insert at middle");
printf("\n4.) delete from end\n5.) reverse the link list\n6.) display list\n7.)exit");
while(1)
{
printf("\n\n Enter the choice of operation you want to do ");
scanf("%d",&i);
switch(i)
{
case 1:
{
printf("Enter the value you want to insert in node ");
scanf(" %s",&words);
insert_beginning(words);
display();
break;
}
I just want to be sure if these 3 parts are correct. 我只想确定这三个部分是否正确。 Am I missing something?
我想念什么吗? Appreciate the help.
感谢帮助。 I hope i didn't complicate anything and asked the question as needed to be asked.
我希望我没有复杂化,并根据需要提出问题。
Your function insert_beginning
has a problem : 您的函数
insert_beginning
有问题:
var=(struct node *)malloc(sizeof(struct node));
var->data[100]=words[100]
By these two lines you want make a copy of words
array and put it on the node of your list :) But you implementation is false, you just copy the 100th char
(Which is out of bounds of the array cuz the first char begins at 0 and the last is at the 99 index) from the words
array to the node data
100th char. 通过这两行,您想复制
words
数组并将其放在列表的节点上:)但是您的实现是错误的,您只需复制第100个char
(第一个字符开始于数组cuz的范围之外) 0,最后一个在99索引处),从words
数组到节点data
第100个字符。
You have to copy all the words
content, so you can use strncpy
function to do it : 您必须复制所有
words
内容,因此可以使用strncpy
函数进行操作:
strncpy(var->data, words, 100);
so your function will seems like that : 所以你的功能看起来像这样:
void insert_beginning(char words[100])
{
struct node *var, *temp;
var=(struct node *)malloc(sizeof(struct node));
strncpy(var->data, words, 100);
if (head==NULL)
{
head=var;
head->previous=NULL;
head->next=NULL;
last=head;
}
else
{
temp=var;
temp->previous=NULL;
temp->next=head;
head->previous=temp;
head=temp;
}
}
Don't use strcpy
cuz isn't a safe function against buffer overflows, use strncpy
. 不要使用
strcpy
cuz并不是防止缓冲区溢出的安全函数,请使用strncpy
。
It left to you just to fix the main which i didn't understand what do u want to do it. 它只留给您解决主要问题,我不知道您想做什么。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.