Linked List to Read in a File in C With While Loop

Afterwards arrays, the second most popular data structure is Linked List. A linked listing is a linear data construction, made of a chain of nodes in which each node contains a value and a arrow to the side by side node in the concatenation. In this article, let'southward see how to implement a linked list in C.

What is Linked List in C?

A Linked List is a linear data structure. Every linked listing has two parts, the data section and the address section that holds the address of the next element in the list, which is called a node.

The size of the linked list is not fixed, and data items can exist added at whatsoever locations in the listing. The disadvantage is that to get to a node, we must traverse to all the mode from the first node to the node that we crave. The Linked List is like an assortment but unlike an array, information technology is not stored sequentially in the memory.

The most popular types of a linked listing are:

  1. Singly link list
  2. Doubly link list

Example of Linked List

Format:[information,accost]

Caput->[3,1000]->[43,1001]->[21,1002]

In the example, the number 43 is present at location 1000 and the accost is present at in the previous node. This is how a linked listing is represented.

Basic Linked Listing Functions

At that place are multiple functions that tin can be implemented on the linked list in C. Permit'southward attempt to understand them with the assistance of an example program. First, we create a list, display it, insert at any location, delete a location. The following lawmaking volition show you lot how to perform operations on the list.

#include<stdlib.h> #include <stdio.h>      void create(); void display(); void insert_begin(); void insert_end(); void insert_pos(); void delete_begin(); void delete_end(); void delete_pos();   struct node {         int info;         struct node *next; }; struct node *get-go=Nix; int chief()      {         int option;         while(1){                                printf("north                Bill of fare                             north");                 printf("northward 1.Create     northward");                 printf("n 2.Brandish    n");                 printf("n iii.Insert at the beginning    due north");                 printf("n 4.Insert at the terminate  n");                 printf("n five.Insert at specified position       n");                 printf("due north half-dozen.Delete from commencement      n");                 printf("due north 7.Delete from the end        north");                 printf("northward viii.Delete from specified position     n");                 printf("due north 9.Exit       n");                 printf("northward--------------------------------------due north");                 printf("Enter your choice:t");                 scanf("%d",&pick);                 switch(choice)                 {                         case 1:                                         create();                                         break;                         instance 2:                                         brandish();                                         break;                         case iii:                                          insert_begin();                                         interruption;                         case 4:                                         insert_end();                                         break;                         example 5:                                         insert_pos();                                         interruption;                         example vi:                                         delete_begin();                                         break;                         case 7:                                         delete_end();                                         interruption;                         example 8:                                         delete_pos();                                         suspension;                                                 instance 9:                                         leave(0);                                         break;                                                      default:                                         printf("north Wrong Choice:n");                                         break;                 }         }         return 0; } void create() {         struct node *temp,*ptr;         temp=(struct node *)malloc(sizeof(struct node));         if(temp==NULL)         {                 printf("nOut of Retention Infinite:north");                 get out(0);         }         printf("nEnter the information value for the node:t");         scanf("%d",&temp->info);         temp->side by side=NULL;         if(beginning==NULL)         {                 beginning=temp;         }         else         {                 ptr=beginning;                 while(ptr->next!=Naught)                 {                         ptr=ptr->next;                 }                 ptr->next=temp;         } } void display() {         struct node *ptr;         if(offset==Cipher)         {                 printf("nList is empty:n");                 return;         }         else         {                 ptr=start;                 printf("nThe List elements are:northward");                 while(ptr!=NULL)                 {                         printf("%dt",ptr->info );                         ptr=ptr->next ;                 }         } } void insert_begin() {         struct node *temp;         temp=(struct node *)malloc(sizeof(struct node));         if(temp==Goose egg)         {                 printf("nOut of Memory Space:n");                 return;         }         printf("nEnter the information value for the node:t" );         scanf("%d",&temp->info);         temp->next =Nothing;         if(start==Nix)         {                 kickoff=temp;         }         else         {                 temp->adjacent=start;                 first=temp;         } } void insert_end() {         struct node *temp,*ptr;         temp=(struct node *)malloc(sizeof(struct node));         if(temp==NULL)         {                 printf("nOut of Memory Space:n");                 return;         }         printf("nEnter the data value for the node:t" );         scanf("%d",&temp->info );         temp->next =Nix;         if(first==Goose egg)         {                 commencement=temp;         }         else         {                 ptr=first;                 while(ptr->next !=NULL)                 {                         ptr=ptr->next ;                 }                 ptr->next =temp;         } } void insert_pos() {         struct node *ptr,*temp;         int i,pos;         temp=(struct node *)malloc(sizeof(struct node));         if(temp==Nada)         {                 printf("nOut of Memory Infinite:due north");                 return;         }         printf("nEnter the position for the new node to exist inserted:t");         scanf("%d",&pos);         printf("nEnter the data value of the node:t");         scanf("%d",&temp->info) ;           temp->next=NULL;         if(pos==0)         {                 temp->next=start;                 start=temp;         }         else         {                 for(i=0,ptr=start;i<pos-1;i++) { ptr=ptr->next;                         if(ptr==Aught)                         {                                 printf("nPosition not found:[Handle with care]due north");                                 return;                         }                 }                 temp->next =ptr->next ;                 ptr->side by side=temp;         } } void delete_begin() {         struct node *ptr;         if(ptr==NULL)         {                 printf("nList is Empty:north");                 return;         }         else         {                 ptr=start;                 start=start->next ;                 printf("nThe deleted chemical element is :%dt",ptr->info);                 gratis(ptr);         } } void delete_end() {         struct node *temp,*ptr;         if(start==NULL)         {                 printf("nList is Empty:");                 exit(0);         }         else if(commencement->next ==Aught)         {                 ptr=outset;                 get-go=NULL;                 printf("nThe deleted element is:%dt",ptr->info);                 gratuitous(ptr);         }         else         {                 ptr=start;                 while(ptr->next!=Zip)                 {                         temp=ptr;                         ptr=ptr->side by side;                 }                 temp->next=Cypher;                 printf("nThe deleted element is:%dt",ptr->info);                 gratis(ptr);         } } void delete_pos() {         int i,pos;         struct node *temp,*ptr;         if(start==NULL)         {                 printf("nThe List is Empty:due north");                 get out(0);         }         else         {                 printf("nEnter the position of the node to be deleted:t");                 scanf("%d",&pos);                 if(pos==0)                 {                         ptr=start;                         commencement=showtime->adjacent ;                         printf("nThe deleted element is:%dt",ptr->info  );                         gratuitous(ptr);                 }                 else                 {                         ptr=start;                         for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;                                 if(ptr==NULL)                                 {                                         printf("nPosition not Found:n");                                         return;                                 }                         }                         temp->next =ptr->next ;                         printf("nThe deleted element is:%dt",ptr->info );                         complimentary(ptr);                 }         } }        

The first part of this code is creating a construction. A linked listing structure is created and then that information technology can hold the data and accost as nosotros need it. This is done to give the compiler an idea of how the node should exist.

struct node {         int info;         struct node *next; };        

In the structure, we have a data variable chosen info to hold data and a pointer variable to point at the address. In that location are diverse operations that can exist washed on a linked list, like:

  • create()
  • brandish()
  • insert_begin()
  • insert_end()
  • ]insert_pos()
  • delete_begin()
  • delete_end()
  • delete_pos()

These functions are called by the bill of fare-driven main function. In the primary function, nosotros take input from the user based on what operation the user wants to do in the programme. The input is then sent to the switch case and based on user input.

Based on what input is provided the function will be called. Next, we have dissimilar functions that need to be solved. Let'south take a look at each of these functions.

Create Part

First, there is a create office to create the linked list. This is the basic way the linked listing is created. Lets us await at the code.

void create() {         struct node *temp,*ptr;             printf("nEnter the data value for the node:t");         scanf("%d",&temp->info);         temp->next=NULL;         if(start==Aught)         {                 start=temp;         }         else         {                 ptr=offset;                 while(ptr->next!=NULL)                 {                         ptr=ptr->next;                 }                 ptr->side by side=temp;         } }        

Output

Insert - Linked List - Edureka

First, two pointers are created of the type node, ptr, and temp . We have the value that is needed to exist added in the linked list from the user and store it in info part of the temp variable and assign temp of next that is the accost part to nix. In that location is a first arrow holding the start of the list. Then we check for the start of the list. If the first of the list is null, and so we assign temp to the start pointer. Otherwise, we traverse to the last point where the information has been added.

For this, we assign ptr the offset value and traverse till ptr->next= null . We then assign ptr->next the address of temp. In a similar way, there is lawmaking given for inserting at the beginning, inserting at the end and inserting at a specified location.

Display Office

Here's the code for display office.

void display() {         struct node *ptr;         if(start==Naught)         {                 printf("nList is empty:n");                 return;         }         else         {                 ptr=start;                 printf("nThe Listing elements are:n");                 while(ptr!=NULL)                 {                         printf("%dt",ptr->info );                         ptr=ptr->side by side ;                 }         } }        

Output

Display - Linked List - Edureka

In the brandish office, nosotros start check if the listing is empty and return if it is empty. In the next part, nosotros assign the offset value to ptr. We then run a loop till ptr is null and print the data element for each node, until ptr is null, which specifies the end of the list.

Delete Function

Here' south the snippet of code to delete a node from the linked list.

void delete_pos() {         int i,pos;         struct node *temp,*ptr;         if(beginning==NULL)         {                 printf("nThe List is Empty:northward");                 exit(0);         }         else         {                 printf("nEnter the position of the node to be deleted:t");                 scanf("%d",&pos);                 if(pos==0)                 {                         ptr=showtime;                         get-go=start->next ;                         printf("nThe deleted element is:%dt",ptr->info  );                         free(ptr);                 }                 else                 {                         ptr=showtime;                         for(i=0;i<pos;i++) { temp=ptr; ptr=ptr->next ;                                 if(ptr==NULL)                                 {                                         printf("nPosition non Found:n");                                         return;                                 }                         }                         temp->next =ptr->next ;                         printf("nThe deleted element is:%dt",ptr->info );                         free(ptr);                 }         } }        

Output

Delete- Linked List - Edureka

In the deletion procedure, it first checks if the list is empty, if yes it exists. If information technology is not empty it asks the user for the position to be deleted. Once the user enters the position, it checks if it is the first position, if yes it assigns ptr to commencement and moves the start pointer to side by side location and deletes ptr. If the position is non zero, and then it runs a for loop from 0 all the way to the pos entered by the user and stored in the pos variable. There is an if statement to make up one's mind if the position entered is not present. If ptr is equal to nil, then it is not nowadays.

Nosotros assign ptr to temp in the for loop, and ptr then moves on to the next role. After this when the position is found. We make the temp variable to hold the value of ptr->side by side thus skipping the ptr. Then ptr is deleted. Similarly, information technology can be done for starting time and the terminal element deletion.

Doubly Linked List

It is called the doubly linked list considering in that location are two pointers, one indicate to the adjacent node and other points to the previous node. The operations performed in doubly linked are similar to that of a singly linked list. Here's the code for basic operations.

#include<stdio.h> #include<stdlib.h> struct Node; typedef struct Node * PtrToNode; typedef PtrToNode List; typedef PtrToNode Position;   struct Node {     int e;     Position previous;     Position next; };   void Insert(int x, List l, Position p) {     Position TmpCell;     TmpCell = (struct Node*) malloc(sizeof(struct Node));     if(TmpCell == Nix)         printf("Retentiveness out of spacen");     else     {         TmpCell->e = x;         TmpCell->previous = p;         TmpCell->adjacent = p->next;         p->side by side = TmpCell;     } }      void Delete(int ten, List fifty) {     Position p, p1, p2;     p = Find(x, fifty);     if(p != Naught)     {         p1 = p -> previous;         p2 = p -> next;         p1 -> next = p -> next;         if(p2 != Zero)                  // if the node is not the concluding node             p2 -> previous = p -> previous;     }     else         printf("Element does not exist!!!n"); }       void Display(Listing fifty) {     printf("The list element are :: ");     Position p = 50->next;     while(p != NULL)     {         printf("%d -> ", p->e);         p = p->next;     } }   int principal() {     int 10, pos, ch, i;     List l, l1;     l = (struct Node *) malloc(sizeof(struct Node));     l->previous = Null;     fifty->next = NULL;     List p = l;     printf("DOUBLY LINKED List IMPLEMENTATION OF LIST ADTnn");     practise     {         printf("nn1. CREATEn 2. DELETEn  iii. DISPLAYn 4. QUITnnEnter the choice :: ");         scanf("%d", &ch);         switch(ch)         {         case 1:             p = l;             printf("Enter the element to be inserted :: ");             scanf("%d",&10);             printf("Enter the position of the chemical element :: ");             scanf("%d",&pos);             for(i = ane; i < pos; i++) { p = p->next;             }             Insert(10,fifty,p);             pause;           instance 2:             p = fifty;             printf("Enter the chemical element to be deleted :: ");             scanf("%d",&10);             Delete(10,p);             break;                      case 3:             Display(l);             break;         }     }     while(ch<4); }        

Output
LinkedList - EdurekaDoubly List - Linked List - Edureka

Then, as you tin meet the concept of operations is quite elementary. The doubly linked listing has the same operations as that of singly linked list in C programming language. The merely difference is that in that location is some other address variable which help is traversing the list meliorate in a doubly linked listing.

I promise you have understood how to perform bones operations on singly and doubly linked list in C.

If y'all wish to learn Linked Listing in Coffee, here'southward a complete guide.

If you come beyond whatsoever questions, feel free to ask all your questions in the comments department of "Linked List in C" and our team will be glad to reply.

owensdanythe.blogspot.com

Source: https://www.edureka.co/blog/linked-list-in-c/

0 Response to "Linked List to Read in a File in C With While Loop"

Post a Comment

Iklan Atas Artikel

Iklan Tengah Artikel 1

Iklan Tengah Artikel 2

Iklan Bawah Artikel