I'm trying to build a doubly linked list in C by inputting strings and I want to be clear if i'm right. 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.
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.