/*
Title: Single Linklist
Aim: To write a C program of single linklist with the following operations:
1. Create a linklist
2. Display the linklist
3. Count the no of nodes in LL
4. search for an element and display its position
5. Add a node at the end, at the beginning, after a node, before a node, at a particular position
6. Delete a node
7. Reverse the linklist
*/
#include<stdio.h>
#include<stdlib.h>
struct node
{
    int data;
    struct node *link;
}*start;
int n;
void display()
{
    struct node *p;
    if(start == NULL)
    {
        printf("\n List is empty\n");
        return;
    }
    else
    {
        printf("\n The List is: ");
        for(p=start ; p!=NULL ; p=p->link)
        printf("%d ",p->data);
        printf("\n");
    }
}
void dispfile(FILE *fp)
{
    struct node *p;
    if(start == NULL)
    fprintf(fp, "\nThe List is empty\n");
    else
    {
        fprintf(fp, "\nThe list is: ");
        for(p=start; p!=NULL; p=p->link)
        fprintf(fp, "%d ",p->data);
    }
}
void create(FILE *fp)
{
    int i;
    struct node *temp, *p;
    printf(" ______Creating a List______\n");
    fprintf(fp, "\n ______Creating a List______\n");
    if(start == NULL)
    {
        printf(" Enter the number of nodes to create: ");
        scanf("%d",&n);
        temp = (struct node *)malloc(sizeof(struct node));
        printf(" Enter the data for node 1: ");
        scanf("%d",&temp->data);
        start = temp;
        start->link = NULL;
        for(i=1, p = start ; i<n ; i++)
        {
            temp = (struct node *)malloc(sizeof(struct node));
            printf(" Enter the data for node %d: ",i+1);
            scanf("%d",&temp->data);
            p->link = temp;
            temp->link = NULL;
            p = temp;
        }
        dispfile(fp);
    }
    else
    {
        printf("List is already created, use insert functions");
        fprintf(fp, "\n*List is already created*\n");
    }
}

void count(FILE *fp)
{
    int i;
    struct node *p;
    printf(" ______Counting the Nodes______\n");
    fprintf(fp, "\n\n ______Counting the Nodes______\n");
    if(start==NULL)
    fprintf(fp, "List is empty\n");
    else
    {
        for(p=start, i=0; p!=NULL; p=p->link,i++);
        printf("\nThe list contains %d nodes",i);
        fprintf(fp,"The list contains %d nodes\n",i);
        dispfile(fp);
    }
    
}
void search(FILE *fp)
{
    int ch, i;
    struct node *p;
    printf(" ______Searching a Node______\n");
    fprintf(fp,"\n\n ______Searching a Node______\n");
    printf("Enter the data to search: ");
    scanf("%d",&ch);
    fprintf(fp,"Input = %d\n",ch);
    for(p=start, i = 1; p!=NULL; p=p->link, i++)
    {
        if(p->data == ch)
        {
            printf(" Node with data %d is at position %d\n",p->data,i);
            fprintf(fp, "Node with data %d is at position %d\n", p->data, i);
            break;
        }
    }
    if(start==NULL)
    fprintf(fp, "List is empty\n");
    else if(p == NULL)
    {
        printf(" Node not found\n");
        fprintf(fp,"Node not found\n");
    }
}
void add_e(FILE *fp)
{
    struct node *p, *temp;
    printf(" ______Adding at End______\n");
    fprintf(fp,"\n ______Adding at End______\n");
    temp = (struct node *)malloc(sizeof(struct node));
    printf(" Enter the data for node : ");
    scanf("%d",&temp->data);
    fprintf(fp, "Input = %d\n",temp->data);
    if(start == NULL)
    {
        start = temp;
        temp->link = NULL;
        n=1;
        dispfile(fp);
    }
    else
    {
        for(p=start ; p->link!=NULL ; p=p->link);
        p->link = temp;
        temp->link = NULL;
        n++;
        dispfile(fp);
    }
}
void add_beg(FILE *fp)
{
    struct node *temp;
    temp = (struct node *)malloc(sizeof(struct node));
    printf("______Adding at the beginning______\n");
    fprintf(fp, "\n\n______Adding at the beginning______\n");
    printf(" Enter the data for node : ");
    scanf("%d",&temp->data);
    fprintf(fp, "Input = %d\n",temp->data);
    if(start==NULL)
    {
        start = temp;
        temp->link = NULL;
        n=1;
        dispfile(fp);
    }
    else
    {
        temp->link = start;
        start = temp;
        n++;
        dispfile(fp);
    }
}
void add_af(FILE *fp)
{
    struct node *temp, *p;
    int x;
    printf(" ______Adding after a Node______\n");
    fprintf(fp, "\n\n______Adding After a node______\n");
    if(start==NULL)
    fprintf(fp, "List is empty\n");
    else
    {
        printf(" Enter the data of a node to insert after it: ");
        scanf("%d",&x);
        fprintf(fp, "Input node = %d\n",x);
        for(p=start; p!=NULL; p=p->link)
        if(p->data == x)
        break;
        if(p==NULL)
        {
            printf(" Node not found\n");
            fprintf(fp, "Node not found\n");
        }
        else
        {
            temp = (struct node *)malloc(sizeof(struct node));
            printf(" Enter the data for node : ");
            scanf("%d",&temp->data);
            fprintf(fp, "Data Input = %d\n",temp->data);
            temp->link = p->link;
            p->link = temp;
            n++;
            dispfile(fp);
        }
        
    }
}
void add_bef(FILE *fp)
{
    struct node *temp, *p;
    int x;
    printf("______Adding Before a Node______\n");
    fprintf(fp, "\n\n______Adding Before a Node______\n");
    if(start==NULL)
    fprintf(fp, "List is empty\n");
    else
    {
        printf(" Enter the data of a node to insert before it: ");
        scanf("%d",&x);
        fprintf(fp, "Input node = %d\n",x);
        for(p=start; p->link!=NULL; p=p->link)
        if(p->link->data == x)
        break;
        if(p->link==NULL)
        {
            printf("Node not found\n");
            fprintf(fp, "Node not found\n");
        }
        else
        {
            temp = (struct node *)malloc(sizeof(struct node));
            printf(" Enter the data for node : ");
            scanf("%d",&temp->data);
            fprintf(fp, "Data Input = %d\n",temp->data);
            temp->link = p->link;
            p->link = temp;
            n++;
            dispfile(fp);
        }
    }
}
void add_p(FILE *fp)
{
    int pos, i;
    struct node *temp, *p;
    printf("______Inserting at given position______\n");
    fprintf(fp, "\n\n______Inserting at given position______\n");
    printf(" Enter the position to insert a node: ");
    scanf("%d",&pos);
    fprintf(fp, "Position to input = %d\n",pos);
    if(pos>n+1)
    {
        printf(" List is too short\n");
        fprintf(fp,"List is too short\n");
    }
    else
    {
        temp = (struct node *)malloc(sizeof(struct node));
        printf(" Enter the data for node : ");
        scanf("%d",&temp->data);
        fprintf(fp, "Data input = %d\n",temp->data);
        if(start == NULL)
        {
            start = temp;
            temp->link = NULL;
            n=1;
            dispfile(fp);
        }
        else
        {
            for(i=1, p=start ; p!=NULL && i<pos-1 ; i++, p=p->link);
            temp->link = p->link;
            p->link = temp;
            n++;
            dispfile(fp);
        }
    }
}
void del(FILE *fp)
{
    struct node *p, *q;
    int pos, dat, i, flag = 0;
    printf("______Deleting from a given position______\n");
    fprintf(fp, "\n\n______Deleting from a given position______\n");
    if(start==NULL);
    else
    {
        printf("Enter the position and the data at the position to delete: ");
        scanf("%d%d",&pos,&dat);
        fprintf(fp, "Position and data to input = %d, %d\n",pos, dat);
        if(pos == 1 && dat == start->data)
        {
            p =start;
            start = start->link;
            free(p);
            n--;
            flag = 1;
            dispfile(fp);
        }
        for(i=2, p=start; p!=NULL; p=p->link, i++)
        {
            if(i==pos && p->link->data == dat)
            {
                q = p->link;
                p->link = q->link;
                free(q);
                n--;
                flag = 1;
                dispfile(fp);
                break;
            }
        }
        if(flag == 0)
        {
            printf("Node Not Found\n");
            fprintf(fp, "Node Not found\n");
        }
    }
}
void reverse(FILE *fp)
{
    struct node *p1,*p2,*p3;
    printf("______Reversing the List______\n");
    fprintf(fp, "\n\n______Reversing the List______\n");
    if(start==NULL)
    {
        fprintf(fp, "List is empty\n");
        return;
    }
    if(start->link==NULL)
    {
        return;
        dispfile(fp);
    }
    p1=NULL;
    p2=start;
    p3=start->link;
    while(p2 != NULL)
    {
        p2->link=p1;
        p1 = p2;
        p2 = p3;
        if(p3!=NULL)
        p3 = p3->link;
    }
    start = p1;
    printf("List reversed successfully");
    fprintf(fp, "List reversed successfully\n");
    dispfile(fp);
}
int main()
{
    int ch=2;
    FILE *fp;
    fp = fopen("Linklist1.txt","w");
    do
    {
        if(ch!=2)
        display();
        printf("\n\n Enter your choice\n 1. Create\n 2. Display\n 3. Count the no of nodes\n 4. search\n 5. Add a node at the end\n ");
        printf("6. Add at the beginning\n 7. Add after a node\n 8. Add before a node\n 9. Add at a particular position\n ");
        printf("10. Delete a node\n 11. Reverse the linklist\n 12. EXIT\n");
        scanf("%d",&ch);
        switch(ch)
        {
            case 1:system("cls"); create(fp);        break;
            case 2:system("cls"); display();fprintf(fp, "\n\n______Displaying the List______\n");dispfile(fp);    break;
            case 3:system("cls"); count(fp);        break;
            case 4:system("cls"); search(fp);        break;
            case 5:system("cls"); add_e(fp);        break;
            case 6:system("cls"); add_beg(fp);    break;
            case 7:system("cls"); if(start!=NULL)display();add_af(fp);break;
            case 8:system("cls"); if(start!=NULL)display();add_bef(fp);break;
            case 9:system("cls"); display();add_p(fp);        break;
            case 10:system("cls");if(start!=NULL)display();del(fp);break;
            case 11:system("cls");reverse(fp);    break;
            case 12:fclose(fp);exit(1);
            default:system("cls");printf("Invalid Choice\n");break;
        }
    }while(ch != 12);
}

 
by

C Language online compiler

Write, Run & Share C Language code online using OneCompiler's C online compiler for free. It's one of the robust, feature-rich online compilers for C language, running the latest C version which is C18. Getting started with the OneCompiler's C editor is really simple and pretty fast. The editor shows sample boilerplate code when you choose language as 'C' and start coding!

Read inputs from stdin

OneCompiler's C online editor supports stdin and users can give inputs to programs using the STDIN textbox under the I/O tab. Following is a sample C program which takes name as input and print your name with hello.

#include <stdio.h>
int main()
{
    char name[50];
    printf("Enter name:");
    scanf("%s", name);
    printf("Hello %s \n" , name );
    return 0;
    
}

About C

C language is one of the most popular general-purpose programming language developed by Dennis Ritchie at Bell laboratories for UNIX operating system. The initial release of C Language was in the year 1972. Most of the desktop operating systems are written in C Language.

Key features:

  • Structured Programming
  • Popular system programming language
  • UNIX, MySQL and Oracle are completely written in C.
  • Supports variety of platforms
  • Efficient and also handle low-level activities.
  • As fast as assembly language and hence used as system development language.

Syntax help

Loops

1. If-Else:

When ever you want to perform a set of operations based on a condition if-else is used.

if(conditional-expression) {
   // code
} else {
   // code
}

You can also use if-else for nested Ifs and if-else-if ladder when multiple conditions are to be performed on a single variable.

2. Switch:

Switch is an alternative to if-else-if ladder.

switch(conditional-expression) {    
case value1:    
 // code    
 break;  // optional  
case value2:    
 // code    
 break;  // optional  
...    
    
default:     
 // code to be executed when all the above cases are not matched;    
} 

3. For:

For loop is used to iterate a set of statements based on a condition.

for(Initialization; Condition; Increment/decrement){  
  // code  
} 

4. While:

While is also used to iterate a set of statements based on a condition. Usually while is preferred when number of iterations are not known in advance.

while(condition) {  
 // code 
}  

5. Do-While:

Do-while is also used to iterate a set of statements based on a condition. It is mostly used when you need to execute the statements atleast once.

do {
  // code 
} while (condition); 

Arrays

Array is a collection of similar data which is stored in continuous memory addresses. Array values can be fetched using index. Index starts from 0 to size-1.

Syntax

One dimentional Array:

data-type array-name[size];

Two dimensional array:

data-type array-name[size][size];

Functions

Function is a sub-routine which contains set of statements. Usually functions are written when multiple calls are required to same set of statements which increases re-usuability and modularity.

Two types of functions are present in C

  1. Library Functions:

Library functions are the in-built functions which are declared in header files like printf(),scanf(),puts(),gets() etc.,

  1. User defined functions:

User defined functions are the ones which are written by the programmer based on the requirement.

How to declare a Function

return_type function_name(parameters);

How to call a Function

function_name (parameters)

How to define a Function

return_type function_name(parameters) {  
  //code
}