M
Mansoor
Hi,
I recently (1 week) started to learn C language. I have been reading a
book about C and I've been using Turbo C++ in DOS. After I learnt the
basic concepts I decided to write a demo program which is about a very
simple linked-list managing.
I've included my code here. I just want to ask those who are
experienced in C , to take a bit of their time, have a look at this
code and write bac for me, what problems and error it has (in
programming techniques view like commenting, organizing the code,
etc). I believe if I write millions of lines but nobody tell me my
mistakes, I won't learn anything.
Code:
/* A Linked List Manager with Add, Delete, Display */
/* functionalities. */
#include <stdio.h>
#include <stdlib.h>
/* Function Prototypes */
char get_comm(struct s_l_list *ll_p);
void show_list(struct s_l_list *ll_p);
void add_elem(struct s_l_list *ll_p);
struct s_ll_rec *search_elem(struct s_l_list *ll_p, unsigned int id);
void del_elem(struct s_l_list *ll_p);
/* ------------- */
/* Data Storages */
/* ------------- */
/* Phone book record */
struct s_user_rec
{
unsigned int id; /* User ID */
char *username, /* User Name */
*phone; /* User Phone Number */
};
/* Linked list's record */
struct s_ll_rec
{
struct s_user_rec *data;/* Phone Book Record */
struct s_ll_rec *next; /* Next Linked List Record */
};
/* Linked list */
struct s_l_list
{
struct s_ll_rec *head; /* First List Element */
int len; /* Number of Records */
};
typedef struct s_user_rec user_rec;
typedef struct s_l_list l_list;
typedef struct s_ll_rec ll_rec;
/* Main Program */
main()
{
char com; /* command temp */
l_list list = { NULL , 0 }; /* Linked List */
/* Input Command and call functions */
while (1)
{
com = get_comm(&list); /* Get Command */
switch (com)
{
case 'D':
{
show_list(&list);
break;
}
case 'A':
{
add_elem(&list);
break;
}
case 'R':
{
del_elem(&list);
break;
}
case 'S':
{
}
case 'Q':
{
/* free the memory reserved for list */
if (list.head != NULL)
{
ll_rec *lt = list.head;
do
{
free(lt);
} while ((lt = lt->next) != NULL);
}
printf("\nThanks for using our program.");
exit(0);
}
}
}
return 0;
}
/* Diplaying the command list and read the commmand.
Prototype: char get_comm(l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: the command character:
'D': display the list
'A': add an element to the list
'R': remove an element from the list
'S': search an element through the list
'Q': quit the program */
char get_comm(struct s_l_list *ll_p)
{
char buf[80]; /* Input Buffer */
/* Display the Command List */
while (1)
{
puts("\nCommand List:");
printf("\tCurrent list members: %d", ll_p->len);
printf("\n\t(D) Display the whole list.\
\n\t(A) Add an element to list.\
\n\t(R) Remove an element from list.\
\n\t(S) Search for an element.\
\n\t(Q) Quit the program.\
\n>> ");
gets(buf);
/* Check for command */
switch (buf[0])
{
case 'D':
;
case 'A':
;
case 'R':
;
case 'S':
;
case 'Q':
return buf[0];
}
}
}
/* Showing the entire list
Protoptype: void show_list(struct s_l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: none */
void show_list(struct s_l_list *ll_p)
{
/* Check if list is empty or not */
if (ll_p->head != NULL)
{
struct s_ll_rec *cur = ll_p->head; /* Current Record */
/* Print Heading */
printf("\nLinked List:");
do
{
printf("\n (%s) has ID of (%u) and no. of (%s).",
cur->data->username, cur->data->id, cur->data->phone);
} while ((cur = cur->next) != NULL);
}
}
/* Add an element to linked list
Prototype: void add_elem(struct s_l_list *ll_p)
Arguments: ll_p: pointer to link list
Returns: none */
void add_elem(struct s_l_list *ll_p)
{
static unsigned long id;/* ID generator var */
user_rec *user; /* User Information Storage */
ll_rec *llr_p; /* List Record */
/* Set aside memory for objects */
user = (user_rec *) malloc(sizeof(user_rec));
llr_p = (ll_rec *) malloc(sizeof(ll_rec));
user->username = (char *) malloc(sizeof(char) * 20);
user->phone = (char *) malloc(sizeof(char) * 20);
/* Input the information from user */
printf("\nAdding a new record to list:\
\n Enter your username:");
scanf("%s", user->username);
printf("\n Enter your phone:");
scanf("%s", user->phone);
/* Add the record */
user->id = id > 100000 ? (id = 0) : id++;
llr_p->data = user;
llr_p->next = ll_p->head; /* set new element's following to first
element */
ll_p->head = llr_p; /* set first element to new one */
ll_p->len += 1; /* increase lenght by 1 */
/* Output success message */
printf("\n New record was successfully added to list.\
\n Record Info:\
\n ID: %u\
\n Username: %s\
\n Phone Number: %s", user->id, user->username, user->phone);
}
/* Search id element in a link list and return its first occurnce.
Prototype: struct s_ll_rec *search_elem(struct s_l_list *ll_p
, unsigned int id)
Arguments: ll_p: pointer to link list
id: id to search
Returns: a pointer to the found record, otherwise NULL */
struct s_ll_rec *search_elem(struct s_l_list *ll_p, unsigned int id)
{
/* check if list is empty or not */
if (ll_p->head == NULL)
return NULL;
struct s_ll_rec *cur = ll_p->head; /* Current record */
do
{
/* if id matches, return the pointer */
if (cur->data->id == id)
return cur;
} while ((cur = cur->next) != NULL);
/* otherwise return NULL */
return NULL;
}
/* Delete an element from list.
Prototype: void del_elem(struct s_l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: none */
void del_elem(struct s_l_list *ll_p)
{
char sucess = 0; /* flag var */
unsigned int find_id; /* temp id var to hold search id */
struct s_ll_rec *rec_t, /* pointer to hold the found result, */
*prev; /* pervious one */
/* Input the id to search */
printf("\nEnter the ID to delete:");
scanf("%u", &find_id);
/* check if the record with find_id exists */
if ((rec_t = search_elem(ll_p, find_id)) != NULL)
{
/* check if it is the first record */
if (ll_p->head == rec_t)
ll_p->head = rec_t->next;
/* if not, get the previous record */
else
{
prev = ll_p->head;
do
if (prev->next == rec_t)
{
prev->next = rec_t->next;
sucess = 1; /* End the loop */
}
while (((prev = prev->next) != NULL) && (sucess == 0));
}
/* free the memory occupied by the record */
free(rec_t);
ll_p->len -= 1; /* decrease list's length by 1 */
printf("\nRecord was successfully deleted.");
}
/* if record doesn't exist, show a message saying so */
else
printf("\nNo record with ID %u was found.", find_id);
}
End of Code
I recently (1 week) started to learn C language. I have been reading a
book about C and I've been using Turbo C++ in DOS. After I learnt the
basic concepts I decided to write a demo program which is about a very
simple linked-list managing.
I've included my code here. I just want to ask those who are
experienced in C , to take a bit of their time, have a look at this
code and write bac for me, what problems and error it has (in
programming techniques view like commenting, organizing the code,
etc). I believe if I write millions of lines but nobody tell me my
mistakes, I won't learn anything.
Code:
/* A Linked List Manager with Add, Delete, Display */
/* functionalities. */
#include <stdio.h>
#include <stdlib.h>
/* Function Prototypes */
char get_comm(struct s_l_list *ll_p);
void show_list(struct s_l_list *ll_p);
void add_elem(struct s_l_list *ll_p);
struct s_ll_rec *search_elem(struct s_l_list *ll_p, unsigned int id);
void del_elem(struct s_l_list *ll_p);
/* ------------- */
/* Data Storages */
/* ------------- */
/* Phone book record */
struct s_user_rec
{
unsigned int id; /* User ID */
char *username, /* User Name */
*phone; /* User Phone Number */
};
/* Linked list's record */
struct s_ll_rec
{
struct s_user_rec *data;/* Phone Book Record */
struct s_ll_rec *next; /* Next Linked List Record */
};
/* Linked list */
struct s_l_list
{
struct s_ll_rec *head; /* First List Element */
int len; /* Number of Records */
};
typedef struct s_user_rec user_rec;
typedef struct s_l_list l_list;
typedef struct s_ll_rec ll_rec;
/* Main Program */
main()
{
char com; /* command temp */
l_list list = { NULL , 0 }; /* Linked List */
/* Input Command and call functions */
while (1)
{
com = get_comm(&list); /* Get Command */
switch (com)
{
case 'D':
{
show_list(&list);
break;
}
case 'A':
{
add_elem(&list);
break;
}
case 'R':
{
del_elem(&list);
break;
}
case 'S':
{
}
case 'Q':
{
/* free the memory reserved for list */
if (list.head != NULL)
{
ll_rec *lt = list.head;
do
{
free(lt);
} while ((lt = lt->next) != NULL);
}
printf("\nThanks for using our program.");
exit(0);
}
}
}
return 0;
}
/* Diplaying the command list and read the commmand.
Prototype: char get_comm(l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: the command character:
'D': display the list
'A': add an element to the list
'R': remove an element from the list
'S': search an element through the list
'Q': quit the program */
char get_comm(struct s_l_list *ll_p)
{
char buf[80]; /* Input Buffer */
/* Display the Command List */
while (1)
{
puts("\nCommand List:");
printf("\tCurrent list members: %d", ll_p->len);
printf("\n\t(D) Display the whole list.\
\n\t(A) Add an element to list.\
\n\t(R) Remove an element from list.\
\n\t(S) Search for an element.\
\n\t(Q) Quit the program.\
\n>> ");
gets(buf);
/* Check for command */
switch (buf[0])
{
case 'D':
;
case 'A':
;
case 'R':
;
case 'S':
;
case 'Q':
return buf[0];
}
}
}
/* Showing the entire list
Protoptype: void show_list(struct s_l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: none */
void show_list(struct s_l_list *ll_p)
{
/* Check if list is empty or not */
if (ll_p->head != NULL)
{
struct s_ll_rec *cur = ll_p->head; /* Current Record */
/* Print Heading */
printf("\nLinked List:");
do
{
printf("\n (%s) has ID of (%u) and no. of (%s).",
cur->data->username, cur->data->id, cur->data->phone);
} while ((cur = cur->next) != NULL);
}
}
/* Add an element to linked list
Prototype: void add_elem(struct s_l_list *ll_p)
Arguments: ll_p: pointer to link list
Returns: none */
void add_elem(struct s_l_list *ll_p)
{
static unsigned long id;/* ID generator var */
user_rec *user; /* User Information Storage */
ll_rec *llr_p; /* List Record */
/* Set aside memory for objects */
user = (user_rec *) malloc(sizeof(user_rec));
llr_p = (ll_rec *) malloc(sizeof(ll_rec));
user->username = (char *) malloc(sizeof(char) * 20);
user->phone = (char *) malloc(sizeof(char) * 20);
/* Input the information from user */
printf("\nAdding a new record to list:\
\n Enter your username:");
scanf("%s", user->username);
printf("\n Enter your phone:");
scanf("%s", user->phone);
/* Add the record */
user->id = id > 100000 ? (id = 0) : id++;
llr_p->data = user;
llr_p->next = ll_p->head; /* set new element's following to first
element */
ll_p->head = llr_p; /* set first element to new one */
ll_p->len += 1; /* increase lenght by 1 */
/* Output success message */
printf("\n New record was successfully added to list.\
\n Record Info:\
\n ID: %u\
\n Username: %s\
\n Phone Number: %s", user->id, user->username, user->phone);
}
/* Search id element in a link list and return its first occurnce.
Prototype: struct s_ll_rec *search_elem(struct s_l_list *ll_p
, unsigned int id)
Arguments: ll_p: pointer to link list
id: id to search
Returns: a pointer to the found record, otherwise NULL */
struct s_ll_rec *search_elem(struct s_l_list *ll_p, unsigned int id)
{
/* check if list is empty or not */
if (ll_p->head == NULL)
return NULL;
struct s_ll_rec *cur = ll_p->head; /* Current record */
do
{
/* if id matches, return the pointer */
if (cur->data->id == id)
return cur;
} while ((cur = cur->next) != NULL);
/* otherwise return NULL */
return NULL;
}
/* Delete an element from list.
Prototype: void del_elem(struct s_l_list *ll_p)
Arguments: ll_p: pointer to linked list
Returns: none */
void del_elem(struct s_l_list *ll_p)
{
char sucess = 0; /* flag var */
unsigned int find_id; /* temp id var to hold search id */
struct s_ll_rec *rec_t, /* pointer to hold the found result, */
*prev; /* pervious one */
/* Input the id to search */
printf("\nEnter the ID to delete:");
scanf("%u", &find_id);
/* check if the record with find_id exists */
if ((rec_t = search_elem(ll_p, find_id)) != NULL)
{
/* check if it is the first record */
if (ll_p->head == rec_t)
ll_p->head = rec_t->next;
/* if not, get the previous record */
else
{
prev = ll_p->head;
do
if (prev->next == rec_t)
{
prev->next = rec_t->next;
sucess = 1; /* End the loop */
}
while (((prev = prev->next) != NULL) && (sucess == 0));
}
/* free the memory occupied by the record */
free(rec_t);
ll_p->len -= 1; /* decrease list's length by 1 */
printf("\nRecord was successfully deleted.");
}
/* if record doesn't exist, show a message saying so */
else
printf("\nNo record with ID %u was found.", find_id);
}
End of Code