i update my post and still have some problems with what it prints me
case 1 is to add a newnode
case 2 to print the list
case 3 to print one element(asks to give an index) of the list and delete it
case 4 to find the length of a string that i gave his index
it prints me
enter a choice
1
enter string
hello
enter a choice
1
enter string
there
enter a choice
you
enter string
so in enter choice i write "you" and then it didn't tell me again enter choice but enter string.why?
the hole code(a little big)
Code:
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
#include <string.h>
struct node{
char data[64];
struct node *next;
};
struct node *firsta, *currenta, *newa;
void addnew(void);
void printall(struct node *head);
void printanddelete( int i);
int findlen(int j);
int slen(char *p);
int main(void)
{
int k,i,j,m;
char ch;
firsta=NULL;
do{
printf("enter a choice\n");
scanf("%d", &k);
switch(k){
case 1:
printf("enter string\n");
while ((ch = getchar()) != '\n' && ch != EOF);
addnew();
break;
case 2:
printall(firsta);
break;
case 3:
printf("enter a number");
scanf("%d", &i);
printanddelete(i);
break;
case 4:
printf("enter index to find length");
scanf("%d", &j);
m=findlen(j);
printf("%d", m);}
}while(k!=0);
return 0;
}
void addnew(void)
{
newa=(struct node *)malloc(sizeof(struct node));
if (firsta==NULL)
firsta=currenta=newa;
else
{
currenta=firsta;
while(currenta->next!=NULL)
currenta=currenta->next;
currenta->next=newa;
currenta=newa;
}
gets(currenta->data);
currenta->next=NULL;
}
void printall(struct node *head)
{
struct node *current=head;
if(head==NULL)
return;
while(current!=NULL)
{printf(current->data);
printf("\n");
current=current->next;
}
printf("~");
}
void printanddelete( int i)
{
int index=1;
struct node *current=firsta;
struct node *prev;
while (current!=NULL){
if(i==index)
{printf(current->data);
if(current==firsta)
firsta=current->next;
else
prev=current->next;
free(current);
current=prev;
break;}
else{
index++;
current=current->next;}
}
}
int findlen(int j)
{
struct node *current=firsta;
int index=1;
int l;
while (current!=NULL){
if (j==index)
return(slen(current->data));
else
index++;
current=current->next;
}
}
int slen(char *p)
{
if (*p=='\0')
return 0;
else
return(slen(++p) +1);
}