Thursday, January 6, 2011

LinkedList in C


In my University days, C language was a miserable experience. We were given few assignments and live practical exams. I never learnt the art of debugging as it looked impossible. But yesterday I had to write a simple application on C (Don't ask me why I wanted to get into the nightmare yet again :)).

I started to work on it yesterday night & I was really felt bad. Firstly, I looked for the plugin in Eclipse. There you go.. I downloaded CDT plugin. It was quite a handy to use with its' highlighting feature.
That assignment was all about using the LinkedLists and Queues. I did not use LinkedList in my day-to-day work. But theory was in my mind as clear as a crystal. Thank god it saved few minutes :). So I just searched through the web to see the possible examples on it. I feel C is the best possible language to explain the linkedlist appropriately. Finally I was able to manage to develop the functions on the LinkedList as shown below.


1. Firstly you need to define the struct of that list,



struct ContactInfo {
char name[20];
char contactNo[40];
struct ContactInfo * next;
};

In this structure you need to keep the required attributes such as name[20], contactNo[40] and finally you need to keep the pointer to the next element in the linkedList. Please refer the http://en.wikipedia.org/wiki/Linked_list for more about LinkedLists.



2. Don't forget to define the type definition at the top,



typedef struct ContactInfo info;


3. Define the 2 pointer for keeping track of the head of the list and next position of the list to be add the new elements.



info *curr, *head;

4. Now let's add a element to the list

addContactsMenu() {
// allocate memory for the new element
curr = (info *) malloc(sizeof(info));

// ask to input the new value from user (for name)
char name[20];
printf("Enter name:\n");
scanf("%s", &name);
strcpy(curr->name, name);

// ask to input the new value from user (for contact number)
char contactNo[40];
printf("Contact Number:\n");
scanf("%s", &contactNo);
strcpy(curr->contactNo, contactNo);

// set the next pointer to the current head element
curr->next = head;
// now head element will be the just added element
head = curr;
}

5. Traverse through the linkedlist



displayAllContacts() {
int i = 1;
// set the current element to the head element
curr = head;
while (curr) {
printf("------Contact %d--------\n", i++);
printf("Contact Name : %s\n", curr->name);
printf("Contact Number: %s\n", curr->contactNo);
printf("------Contact End--------\n\n");
curr = curr->next;
}
}

6. Now let's try to delete an element from the list. It is quite interesting. Here we are trying to delete a particular element, which matches with a given name.





deleteContactMenu() {
info *prev;
char name[20];

// user is asked for a name to delete the element
printf("Phone number Search :\n");
printf("Please Enter the first Name:\n");
scanf("%s", &name);

int flag1 = 0;
int flag2 = 0;
curr = head;
while (curr) {
// string compare function used to match the value
if (strcmp(name, curr->name) == 0) {
printf("Current values are :\n");
printf("Contact Number: %s\n", curr->contactNo);
printf("Contact Category: %s\n", curr->category);
printf("Do you want to delete it: 1. Yes 2. No \n");
scanf("%d", &flag2);
flag1 = 1;
break;
}
// to keep track of the predecessor of the current element
prev = curr;
curr = curr->next;
}
if (flag1 == 0) {
printf("Sorry Contact with that name is not available in the system: \n");

} else if (flag2 == 1) {
// check whether deleted element has a predecessor
if (tmp) {
// check whether deleted element has a point
if (curr->next) {
// swap the pointers, so that predescesor and successor are linked.
// While deleted element does not referred by any other element
tmp->next = curr->next;
curr = curr->next;
} else {
tmp->next = NULL;
}
} else {
head = curr->next;
}
printf("That contact was successfully deleted.");
}

}





No comments:

Post a Comment