Register | Sing In

New posts

Stacks in C programming Linked Lists in C programming File Input/Output in C programming Check prime numbers in C Programming C Programming Enumeration C Programming Union

Archives

2013-10 2013-09 2013-08

Linked Lists in C programming

October 23, 2013 Posted by: GPA Views: 2398 0 comments

A linked list is a linear collection of self-referential structures, called nodes. Linked list is dynamical. A node include data of any type. You can store data in arrays, but linked lists provide several advantages.Linked lists are dynamic, so the length of a list can increase or decrease as necessary. The size of an array can not be altered once memory is allocated. Arrays can become full. Linked lists become full only when the system does not have memory to store link list. In this example we'll create application using link list which stores information about student.



#include <stdio.h>
#include <stdlib.h>
#include <string.h>
struct Student{    
    int id;
    char name[20];
    char email[20];
    double grade;
};
 
struct Node{
   struct Student student;
   struct Node* nextNode;
};
 
//create point to Node
struct Node* startNode=NULL;
 
void InsertNewStudentToList(struct Student student);
void printStudentList();
int deleteStudent(int id);
int isEmpty(struct Node* list);
void printStudentById(int id);
int getId(int id);
 
//main function
int main(int argc, char *argv[])
{
  //demo test
  struct Student student1;
  student1.id=1;
  student1.grade=80;
  strcpy(student1.name,"Peter");
  strcpy(student1.email,"Peter@gmail.com");
 
  struct Student student2;
  student2.id=2;
  student2.grade=50;
  strcpy(student2.name,"John");
  strcpy(student2.email,"John@gmail.com");
 
  InsertNewStudentToList(student1);
  InsertNewStudentToList(student2);
 
  int id;
  char name[20];
  char email[20];
  double grade;
  struct Student newstudent;  
  //allow user to enter new information about student and delete student by id
  int ch=0;
  do{
 
      printf("1 - Add new Student to List");
      printf("\n2 - Delete Student from List");
      printf("\n3 - Show information about all student");
      printf("\n4 - Show information about student by id");
      printf("\n5 - Exit");
      printf("\nSelect one menu item: ");
      scanf("%d",&ch);
      switch(ch){
        case 1:
             printf("Enter student id: ");
             scanf("%d",&id);
             while(getId(id)==1){
                printf("Such id presents in the list, enter student id again: ");      
                scanf("%d",&id);           
             }
             printf("Enter student name: ");
             scanf("%s",&name);
             printf("Enter student email: ");
             scanf("%s",&email);
             printf("Enter student grade: ");
             scanf("%lf",&grade);
             newstudent.id=id;
             newstudent.grade=grade;
             strcpy(newstudent.name,name);
             strcpy(newstudent.email,email);
             InsertNewStudentToList(newstudent);
             printf("\n");
             break;           
        case 2:
             if(isEmpty(startNode)==1){
                printf("\nThe list is empty\n");
             }else{
               printf("Enter student id: ");
                scanf("%d",&id);
               if(deleteStudent(id)==1){
                printf("Student was deleted\n\n");                      
               }else{
                   printf("No such id of student, try again\n\n");
               }      
             }
             break;
        case 3:
             printStudentList();
             break;
        case 4:
             if(isEmpty(startNode)==1){
                printf("\nThe list is empty\n");
             }else{
               printf("Enter student id: ");
               scanf("%d",&id);
               printStudentById(id);     
             }
             break;
      }
  }while(ch!=5);
 
 
  system("PAUSE");	
  return 0;
}//end main function
 
 
void InsertNewStudentToList(struct Student student){
   //insert new student to the list
  struct Node* newNode; //pointer to new node 
  struct Node* previousNode; //pointer to previous node in list
  struct Node* currentNode; //pointer to current node in list  
  newNode = malloc(sizeof(struct Node));
 
  if (newNode != NULL ) {
      newNode->student = student; // place value in node 
      newNode->nextNode = NULL; // node does not link to another node 
      previousNode = NULL;
      currentNode = startNode;
      //sort list by student ID
      while (currentNode != NULL && student.id > currentNode->student.id) {
         previousNode = currentNode;
         currentNode = currentNode->nextNode;
      }
      if (previousNode == NULL ) {
         newNode->nextNode = startNode;
         startNode = newNode;
      } 
      else { //insert new node between previousNode and currentNode 
         previousNode->nextNode = newNode;
         newNode->nextNode = currentNode;
     }
  }else{
    printf( "%d not inserted. No memory available.\n", currentNode->student.id );      
  }     
}
 
void printStudentList(){
 
    struct Node* displayNode;
    if (startNode == NULL ) {
        printf( "List is empty.\n\n" );
    }else {
      printf("\nThe list of students is:\n" );
      displayNode = startNode;
      // while not the end of the list show information about student
      while(displayNode != NULL ) {
         printf("\nID: %d\n", displayNode->student.id );
         printf("Name: %s\n", displayNode->student.name);
         printf("Email: %s\n", displayNode->student.email);
         printf("Grade: %.1lf\n", displayNode->student.grade);
         displayNode = displayNode->nextNode;
       } // end while
       printf("\n\n"); 
    } // end else
}
int getId(int id){
    struct Node* displayNode;
    if (startNode == NULL ) {
        printf( "List is empty.\n\n" );
    }else {
      displayNode=startNode;
      // while not the end of the list show information about student
      while(displayNode != NULL ) {
        if(id==displayNode->student.id){
           return 1;
        }
        displayNode = displayNode->nextNode;
       } // end while 
    } // end else
}
 
void printStudentById(int id){
     struct Node* displayNode;
    if (startNode == NULL ) {
        printf( "List is empty.\n\n" );
    }else {
      displayNode=startNode;
      // while not the end of the list show information about student
      while(displayNode != NULL ) {
        if(id==displayNode->student.id){
            printf("\nID: %d\n", displayNode->student.id );
            printf("Name: %s\n", displayNode->student.name);
            printf("Email: %s\n", displayNode->student.email);
            printf("Grade: %.1lf\n\n", displayNode->student.grade);
        }
         displayNode = displayNode->nextNode;
       } // end while 
    } // end else
}
 
int deleteStudent(int id){
  struct Node* currentNode; //pointer to urrent node 
  struct Node* previousNode; //pointer to previous node in list
  struct Node* tempNode; //pointer to temporary node   
 
  //delete first node 
  if (id == startNode->student.id ) {
        tempNode = startNode; //hold onto node being removed 
        startNode = startNode->nextNode;
        free( tempNode ); // free node 
        return 1;
  }else {
     previousNode = startNode;
     currentNode = startNode->nextNode;
     // loop to find the correct location in the list 
     while ( currentNode != NULL && currentNode->student.id != id ) {
           previousNode = currentNode;
           currentNode = currentNode->nextNode;
     } 
     if ( currentNode != NULL ) {
          tempNode = currentNode;
          previousNode->nextNode = currentNode->nextNode;
          free( tempNode );
          return 1;
     } 
 
   }
}
 
int isEmpty(struct Node* list){
 return list == NULL;//if list is empty return 1(true) else retern 0(false) 
}

Output

Linked Lists in C Programming

Download this example

0 Comments...

Leave a Reply

Please, Sing In to leave a Reply for this material.



Online Chat

LiveZilla Live Help

Popular posts

File Input/Output in C programming Check prime numbers in C Programming Linked Lists in C programming C Programming Union Stacks in C programming Function strtok in C programming

RSS-subscribe

New materials

Subscribe