C-Structure, linked list HELP

i have to create a program for storing data which should.
inserting a person with the given attributes into the list
deleting the person under the specified index
deleting the person with the specified name
displaying the person specified by an index
printing the whole list
Inserting a person involves allocating a structure on the heap and initializing it with some function.
Deleting a person requires freeing unused memory and modifying links in neighbor nodes.


i am having a problem with deleting and adding new entries. if i add more than one entry then one of the entries get scrambled with random values (not exactly random but youll see) and i can only delete if i have more than 2 entries and it also deletes the wrong one.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
#include<stdio.h>
#include<stdlib.h>
#include<malloc.h>
 struct node
  {
     char name[29];
     char sex[10];
     int age;
     int index;
     struct node *next;
  };

  int main()
  {
    int input,index_del, index_sel;
    char temp;
  struct node *h,*t,*t1,*w;
  h=NULL;
while(1)
 {
     
      printf("\n1.display\n2:add\n3.delete\n4.exit\n");
        printf("\nenter your choice=");
      scanf("%d",&input, temp);
      switch(input)
     
{

case 1:    
     
     printf("To display by index number enter: I\n To display all entries enter: A\nEnter A or I: ");
     scanf("%s",&temp);
       if(h==NULL)
   {
    printf("no records are available");
  }
  w=h;
     if(temp == 'A' || temp == 'a'){
  while(w!=NULL)
 {
    printf("\nIndex Number:%d\nName of person:%s\nSex of person:%s\nAge of person:%d\n",
  w->index,w->name,w->sex,w->age);
  w=w->next;
 }
 }else if(temp == 'I' || temp == 'i' && w != NULL)
 {
       printf("\nEnter index number: ");
       scanf("%d",&index_sel);
       t=w;
     while(t->index != index_sel)
   {
     t=t->next;
    }
        printf("\nIndex Number:%d\nName of person:%s\nSex of person:%s\nAge of person:%d\n",
  t->index,t->name,t->sex,t->age);
  }

break;

case 2:
   printf("\nenter the new record=\t");
   if(h==NULL)
 {
  h=t=(struct node *)malloc(sizeof(struct node));//////////////////if more than 2 records added than first record = random values
  printf("\nEnter index number:\t");
  scanf("%d",&t->index);
   printf("\nEnter Name:\t");
    scanf("%s",&t->name);
    printf("\nEnter Sex:\t");
    scanf("%s",&t->sex);
        printf("\nEnter age:\t");
    scanf("%d",&t->age);
   t->next=NULL;
break;
}
else
  {
    t1=(struct node *)malloc(sizeof(struct node));

  printf("\nEnter index number:\t");
  scanf("%d",&t->index);
   printf("\nEnter Name:\t");
    scanf("%s",&t->name);
    printf("\nEnter Sex:\t");
    scanf("%s",&t->sex);
        printf("\nEnter age:\t");
    scanf("%d",&t->age);
    t1->next=t->next;
    t->next=t1;
    t=t1;
                                                                                         
}
break;

case 3:
     ////////////////////// remove by index and by name
    printf("Enter index number of person to be deleted=\n");
    scanf("%d",&index_del);
    t=h;
 while(t->index!=index_del)////////////////////breaks here somewhere
   {
     t=t->next;
    }
     t1=t->next;
     t->next=t1->next;
     free(t1);
 break;

case 4:
 exit(0);
 break;
}
}
}
Topic archived. No new replies allowed.