Salem, It doesn't work.
Code:
#include <stdio.h>#include <stdlib.h>
#include <string.h>
#define MAX_SIZE 30
typedef struct Cities{
char name[20];
int id;
struct Cities *next;
}cityNode;
cityNode *deleteRecord(cityNode *listNode,int pCode);
void traverseTheList(cityNode *Node);
cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter);
char getOption();
void printMenu();
int main()
{
int id, Counter = 0, pCode;
char name[MAX_SIZE], option;
cityNode *linkList = NULL;
static const char exitValue = '4';
printMenu();
do{
switch(option = getOption()){
case '1':
Counter++;
printf("\n");
printf("Sehir plakasi: ");
scanf("%d",&id);
fflush(stdin);
printf("Sehir ismi: ");
gets(name);
fflush(stdin);
linkList = insertNewRecord(linkList,id,name,Counter);
break;
case '2':
Counter--;
printf("\nPlaka Kodu: ");
scanf("%d",&pCode);
linkList = deleteRecord(linkList,pCode);
break;
case '3':
traverseTheList(linkList);
break;
}
}while(exitValue != option);
return 0;
}
void traverseTheList(cityNode *Node){
cityNode *list = Node;
while(list!=NULL){
printf("\n");
printf("%d --> %s",list->id,list->name);
list = list->next;
}
printf("\n");
}
cityNode *deleteRecord(cityNode *listNode,int pCode){
cityNode *previousNode;
cityNode *node = listNode;
if(listNode == NULL){
printf("\nListe bos.\n");
return NULL;
}
if(node->id == pCode){
printf("\nKayit Silindi.\n");
node = listNode;
free(node);
listNode = listNode->next;
return listNode;
}
else{
while(node != NULL && node->id != pCode){
previousNode = node;
node = node->next;
}
if(node == NULL){
printf("Kayit bulunamadi.");
return NULL;
}
else if(node->id == pCode){
printf("\nKayit Silindi.\n");
previousNode->next = node->next;
free(node);
return listNode;
}
}
}
cityNode *insertNewRecord(cityNode *listNode, int id, char name[], int Counter){
int i,j,TEM;
char temp[MAX_SIZE];
cityNode *newRecord = (cityNode*)malloc(sizeof(cityNode));
// cityNode *temp;
newRecord->id = id;
strcpy(newRecord->name,name);
newRecord->next = listNode;
listNode = newRecord;
if(Counter!=1){
for(i = 0; i < Counter-1; i++){
for(j = 0; j < Counter-i-1;j++){
if(strcmp(newRecord[j+1].name,newRecord[j].name) < 0){
strcpy(temp,newRecord[j+1].name);
TEM = newRecord[j+1].id;
strcpy(newRecord[j+1].name,newRecord[j].name);
newRecord[j+1].id = newRecord[j].id;
strcpy(newRecord[j].name,temp);
newRecord[j].id = TEM;
}
}
}
}
return listNode;
}
char getOption(){
char opt;
printf("Secenek: ");
switch(opt = getch()){
case '1':
case '2':
case '3':
case '4':
case '5':return opt;
default:printf("Gecersiz bir islem yapildi\n");
}
return opt;
}
void printMenu(){
printf("----------------------------------\n");
printf("1 - Yeni kayit ekle\n");
printf("2 - Kayit sil\n");
printf("3 - Listeyi goster\n");
printf("4 - Cikis\n");
printf("----------------------------------\n");
}