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 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160
| #include<stdio.h> #include<string.h> #include<stdlib.h> structstud{ charname[10]; structstud*next; }; voidprint(structstud*p); structstud*remove(structstud*p);
structstud*build(){ structstud*current,*nextp,*head; head=(structstud*)malloc(sizeof(structstud)); charstr[10]; printf("typeaname:"); scanf("%s",str); getchar(); strcpy(head->name,str); current=head;
chara; printf("keepdoing?\n"); scanf("%c",&a); while(a=='y'){
printf("typeaname:"); scanf("%s",str); getchar();
nextp=(structstud*)malloc(sizeof(structstud));
strcpy(nextp->name,str); current->next=nextp; current=nextp;
printf("keepdoing?\n"); scanf("%c",&a);
} current->next=NULL; returnhead; }
structstud*insert(structstud*p){ structstud*insert,*current; intposition; charstr[10]; printf("insertposition:\n"); scanf("%d",&position); printf("typeaname:"); scanf("%s",str); getchar();
current=p; insert=(structstud*)malloc(sizeof(structstud)); strcpy(insert->name,str);
insert->next=NULL; if(position!=0){ while(position>1) { current=current->next; position--; } insert->next=current->next; current->next=insert;
} else{ insert->next=current; p=insert;
} printf("\npresentelement:\n"); print(p); returnp; }
intmain(){ structstud*p; p=build(); printf("presentelement:\n"); print(p);
charb='b'; printf("insert?yorn"); scanf("%c",&b); getchar(); while(b!='n'){ p=insert(p); printf("insert?yorn"); scanf("%c",&b);
}
charc; printf("deleteaname?yorn"); scanf("%c",&c); getchar(); while(c!='n'){ p=remove(p); printf("deleteaname?yorn"); scanf("%c",&c); getchar(); } return0;
}
voidprint(structstud*p){ while(1){ if(p->next!=NULL){ printf("%s\n",p->name); p=p->next; } else{ printf("%s\n",p->name); break; } } }
structstud*remove(structstud*p){ structstud*position,*current=p; intpo; printf("place:"); scanf("%d",&po); if(po!=0){ while(po!=1){ current=current->next; po--; } position=current; position=position->next; current->next=position->next;
} else{ p=current->next; free(current); }
printf("currentlist:\n"); print(p); returnp; }
|