Newbi in C
Hi everybody, im new in this forum and new in programing
I hope you can help me with this code, that "simulates" an addresbook, and prints the name, phone, and email on screen from contacts added in nodes.
Thank's
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
using namespace std;
typedef struct _node {
char name[50];
char type[50];
char mail[50];
int phone;
struct _node *next;
} typeNode;
typedef typeNode *pNode;
typedef typeNode *List;
/* List Functions: */
void Insert(List *l, const char *n, const char *t, const char *m, int p);
void Delete(List *l, const char *n, const char *t, const char *m, int p);
int ListEmpty(List l);
void DeleteList(List *);
void ShowList(List l);
int main()
{
List list = NULL;
pNode p;
Insert(&list, "Pepe", "casa", "pepe@hotmail.com", 58221234);
Insert(&list, "Juan", "oficina", "jaumailb@correo.com", 53791234);
Insert(&list, "Carlos", "mobil", "mailc@mail.com", 53101234);
Insert(&list, "Yoda", "casa", "maild@yahoo.com", 555012345);
ShowList(list);
Delete(&list, "Pepe", "casa", "pepe@hotmail.com", 12345);
Delete(&list, "Eusebio", "telefoo", "mil@gmail.com", 5012345);
Delete(&list, "Federico", "telefno", "ail@mail.com", 55432198);
Delete(&list, "Mario", "telfono", "mai@mail.com", 85688722);
Delete(&list, "Juan", "oficina", "jaumailb@correo.com", 53791234);
ShowList(list);
DeleteList(&list);
system("PAUSE");
return 0;
}
void Insert(List *list, const char *n, const char *t, const char *m, int p)
{
pNode newer, previous;
/* Create new node */
newer = (pNode)malloc(sizeof(typeNode));
strcpy( newer->name, n );
strcpy( newer->type, t );
strcpy( newer->mail, m );
newer->phone = p;
/* If list is empty */
if(ListEmpty(*list) || (*list)->name > n) {
/* follow the new node */
newer->next = *list;
/* the beggining of list is newer */
*list = newer;
}
else {
/* follow the node smaller than z */
previous = *list;
/* following */
while(previous->next && previous->next->name <= n)
previous = previous->next;
/* Insert the new node before the previouws node */
newer->next = previous->next;
previous->next = newer;
}
}
void Delete(List *list, const char *n, const char *t, const char *m, int p)
{
pNode previous, node;
node = *list;
previous = NULL;
while(node && node->name < n) {
previous = node;
node = node->next;
}
if(!node || node->name != n) return;
else { /* Delete the node */
if(!previous) /* First element */
*list = node->next;
else /* Any element */
previous->next = node->next;
free(node);
}
}
int ListEmpty(List list)
{
return (list == NULL);
}
void DeleteList(List *list)
{
pNode node;
while(*list) {
node = *list;
*list = node->next;
free(node);
}
}
void ShowList(List list)
{
pNode node = list;
if(ListEmpty(list)) printf("Empty List\n");
else {
while(node) {
printf("%s -> ", node->name);
printf("%s -> ", node->type);
printf("%s -> ", node->mail);
printf("%d \n", node->phone);
node = node->next;
}
printf("\n");
}
}
[Edited by - desmadrator on June 2, 2005 1:49:04 AM]
1: use
[source ] [/source ] tags2: what's the problem you don't excpet us to read your mind? or do you?
one more thing. we need some translation to English. sorry i'm just too lazy to figure out what you trying to do, especially in a different language.
Yeah you'r right, sorry.
Well, my problem now is that the program, prints twice the result on screen, and the "deleted" nodes are really not deleted
the code is in english now, My Aplologies
Well, my problem now is that the program, prints twice the result on screen, and the "deleted" nodes are really not deleted
the code is in english now, My Aplologies
In C, you can't compare strings using operators list < and !=: all this does is compare the pointers. Two strings with identical text my be held at different addresses. In fact, it is most likely.
You'll have to use strcmp().
You'll have to use strcmp().
Why would you pass in so many things to a 'Delete' function when all it needs are the list and a key identifying the item to delete?
This topic is closed to new replies.
Advertisement
Popular Topics
Advertisement