Simple Linked List Example

Posted by Yoel B on 2 Nov 2009, 63 views

 

/*****************************************************************

* L I S T F I N . C                                              *

*                                                                *

* Simple Link-list Program                                       *

*                                                                *

* Written by Yoav Nativ, All rights reserved.                    *

*****************************************************************/

 

#include <string.h>

#include <stdio.h>

#include <alloc.h>

#include <ctype.h>

#include <conio.h>

 

struct Phone{

    char name[20];

    char address[45];

    char number[12];

    };

 

struct Item{

         struct Phone element;

         struct Item *next;

         };

 

void InputPhone(struct Phone *ptr)

{

    printf("Name\t: ");

    flushall();

    gets(ptr->name);

    printf("Address\t: ");

    gets(ptr->address);

    printf("Number\t: ");

    gets(ptr->number);

}

 

void DisplayPhone(struct Phone ph)

{

    printf("%-20s %-45s %s\n", ph.name, ph.address, ph.number);

}

 

 

void InsertItem(struct Phone ph, struct Item **afterptr)

{

   struct Item *temp;

   temp = (struct Item *)malloc(sizeof(struct Item));

   temp->element = ph;

   temp->next = *afterptr;

   *afterptr = temp;

}

 

void DeleteItem(struct Item **afterptr)

{

   struct Item *temp = *afterptr;

   *afterptr = (*afterptr)->next;

   free(temp);

}

 

void CleanList(struct Item **headpp)

{

    while (*headpp != NULL)

        DeleteItem(headpp);

}

 

void DisplayList(struct Item *firstptr)

{

   while(firstptr != NULL) {

       DisplayPhone( firstptr->element );

       firstptr = firstptr->next;

      }

}

 

int ComparePhone(struct Phone ph1, struct Phone ph2)

{

    return strcmp(ph1.name, ph2.name);

}

 

void InsertSorted(struct Phone ph, struct Item **headpp)

{

    while (*headpp != NULL && ComparePhone( (*headpp)->element, ph )<0 )

        headpp = &(*headpp)->next;

 

    InsertItem(ph, headpp);

}

 

void DeleteByValue(char *strname, struct Item **headpp)

{

    while (*headpp != NULL && strcmp(strname, (*headpp)->element.name) != 0)

        headpp = &(*headpp)->next;

 

    if (*headpp != NULL)

        DeleteItem(headpp);

    else

        puts("No Such Item!");

}

 

void DisplayMenu()

{

    puts("\n\nPhone Book Main Menu:");

    puts("=====================\n");

 

    puts("A. Add new name.");

    puts("D. Delete.");

    puts("C. Clean all.");

    puts("P. Print phone book.");

    puts("Q. Quit.");

 

    printf("\nEnter your choice: ");

}

 

void main()

{

    struct Item    *head = NULL;

    struct Phone   temp;

    char           ch;

    char           confirm;

    char           str[20];

 

    do {

        DisplayMenu();

        ch = toupper(getch());

        putchar('\n');

 

        switch (ch) {

            case 'A': InputPhone(&temp);

                      InsertSorted(temp, &head);

                      break;

            case 'D': printf("Name to delete: ");

                      gets(str);

                      DeleteByValue(str, &head);

                      break;

            case 'C': printf("Clean list? ");

                      confirm = toupper(getch());

                      if (confirm=='Y')

                          CleanList(&head);

                      else

                          printf("Operation canceled!");

                      break;

            case 'P': DisplayList(head);

                      break;

            case 'Q': printf("Have a nice day...\n");

                      break;

            default : puts("No such option!!!\n");

            }

    } while (ch != 'Q');

 

    CleanList(&head);

}

 

/*****************************************************************

* LINKLIST . C                                              *

*                                                                *

* Simple Link-list Program                                       *

*                                                                *

* Written by Yoav Nativ, All rights reserved.                    *

*****************************************************************/


 

 

 

 

#include <stdio.h>

#include <alloc.h>

#include <conio.h>

#include <string.h>

 

struct Phone{

    char name[20];

    char address[50];

    long number;

    };

 

void DisplayPhone(struct Phone ph)

{

    printf("%-20s %-50s %ld\n", ph.name, ph.address, ph.number);

}

 

struct Item{

         struct Phone element;

         struct Item *next;

         };

 

void InsertAfter(struct Phone ph, struct Item **afterptr)

{

   struct Item *temp;

   temp = (struct Item *)malloc(sizeof(struct Item));

   temp->element = ph;

   temp->next = *afterptr;

   *afterptr = temp;

}

 

void DeleteAfter(struct Item **afterptr)

{

   struct Item *temp = *afterptr;

   *afterptr = (*afterptr)->next;

   free(temp);

}

 

void DisplayList(struct Item *firstptr)

{

   while(firstptr != NULL) {

       DisplayPhone( firstptr->element );

       firstptr = firstptr->next;

      }

}

 

struct Item **FindItemByName(char *name,struct Item **headpp)

{

   while(*headpp != NULL && strcmp((*headpp)->element.name,name)!=0)

       headpp = &(*headpp)->next;

   return headpp;

}

 

void CleanList(struct Item **headpp)

{

    while (*headpp != NULL)

        DeleteItem(headpp);

}

 

 

 

int HowMany(struct Item *head)

{

    int count = 0;

 

    while (head!=NULL){

        head = head->next;

        count++;

    }

 

    return count;

}

 

void main()

{

    struct Item *head=NULL;

    struct Phone temp;

        clrscr();

    strcpy(temp.name,"Yoav Nativ");

    strcpy(temp.address,"Yahud");

    temp.number=6756534;

    InsertAfter(temp,&head);

 

    strcpy(temp.name,"Yael Nativ");

    strcpy(temp.address,"Yahud");

    temp.number=6621323;

    InsertAfter(temp,&head);

 

    strcpy(temp.name,"Alon Edelman");

    strcpy(temp.address,"Tel-Aviv");

    temp.number=4621323;

    InsertAfter(temp,&head);

 

    strcpy(temp.name,"Pazit Racheli");

    strcpy(temp.address,"Savion");

    temp.number=7823326;

    InsertAfter(temp, &(head->next) );

 

 

 

 

    DisplayList(head);

 

    printf("This list contains: %d items.\n", HowMany(head));

 

    CleanList(&head);

}

 

 

 

/*****************************************************************

* L I S T 1 . C                                                  *

*                                                                *

* Simple Link-list Program                                       *

*                                                                *

* Written by Yoav Nativ, All rights reserved.                    *

*****************************************************************/

 

#include <stdio.h>

#include <alloc.h>

 

struct Phone{

    char name[20];

    char address[45];

    char number[12];

    };

 

struct Item{

         struct Phone element;

         struct Item *next;

         };

 

void InputPhone(struct Phone *ptr)

{

    printf("Name\t: ");

    flushall();

    gets(ptr->name);

    printf("Address\t: ");

    gets(ptr->address);

    printf("Number\t: ");

    gets(ptr->number);

}

 

void DisplayPhone(struct Phone ph)

{

    printf("%-20s %-45s %s\n", ph.name, ph.address, ph.number);

}

 

 

void InsertItem(struct Phone ph, struct Item **afterptr)

{

   struct Item *temp;

   temp = (struct Item *)malloc(sizeof(struct Item));

   temp->element = ph;

   temp->next = *afterptr;

   *afterptr = temp;

}

 

void DeleteItem(struct Item **afterptr)

{

   struct Item *temp = *afterptr;

   *afterptr = (*afterptr)->next;

   free(temp);

}

 

void CleanList(struct Item **headpp)

{

    while (*headpp != NULL)

        DeleteItem(headpp);

}

 

void DisplayList(struct Item *firstptr)

{

   while(firstptr != NULL) {

       DisplayPhone( firstptr->element );

       firstptr = firstptr->next;

      }

}

 

void main()

{

    struct Item  *head = NULL;

    struct Phone temp;

    int    i;

 

    for (i=0 ; i<5 ; i++) {

        InputPhone(&temp);

        InsertItem(temp, &head);

        }

 

    DisplayList(head);

 

    CleanList(&head);

}

 

Subject:
Data Structures (C,C++)
Tags:
Linked List Example C

About Author

Yoel's Classes

No-pails