how to make simple linked list in c?

+1 vote
asked Jan 23 by Dj 1 flag
how to make simple linked list in c?, pls suggest easiest way or other possibilities

3 Answers

0 votes
answered Jan 24 by ramesh (140 points)
//singular link list (insert , delete , search , print)

#include<stdio.h>

#include<stdlib.h>
struct sll
{
    int value;
    struct sll *next;
}*node;
struct sll *first;
void insert(int);
void display();
void Delete(int);
int main()
{
    int i=0;
    while(i!=10){
    insert(50+i);
    i++;}
display();
Delete(52);
printf("\n");
display();

    return 0;
}
void insert(int value)
{
    struct sll * val;
    val=(struct sll*)malloc(1*sizeof(struct sll));
    val->value=value;
    val->next=NULL;
    if(first==NULL)
    {
        first=val;
        node=first;
    }
    else
    {
        node->next=val;
        node=val;
    }
}
void display()
{
    struct sll *ptr;
    ptr=first;
    while(ptr)
    {
        printf("%d--",ptr->value);
        ptr=ptr->next;
    }
}
void Delete(int value)
{
    struct sll *ptr,*ptr1;
    ptr1=first;
    if(ptr1->value==value)
        first=first->next;
    ptr=ptr1->next;
    while(ptr)
    {
        if(ptr->value==value)
            {
                ptr1->next=ptr->next;
                ptr=ptr->next;
            }
            ptr1=ptr;
            ptr=ptr->next;
    }
}
commented Jan 25 by anonymous
Thank You! its very useful for me.
0 votes
answered Feb 4 by manoj
#include<stdio.h>

#include<conio.h>

void main()

{

grtch();

clrscr();

}
0 votes
answered Feb 4 by yaswanth katta
// In this program we are going to perform insert data, delete data and display the data as well.

#include<stdio.h>
#include<stdlib.h>

struct list{
    int data;
    struct list*next;
}*head=NULL;

void insertdata(int value);
void display();
void deletedata();

int main(){
    int choice,num;
    while(1){      //All ways true condition
        printf("1.Insert   2.Delete   3.display   4.exit\n");
        printf("Enter yur choice: ");   // Select the option from the above given
        scanf("%d",&choice);
        
        switch(choice){
            case 1:
                  printf("Enter a number: ");   //To push the number into the list
                  scanf("%d",&num);
                  insertdata(num);
                  break;
            case 2:
                  deletedata();
                  break;
            case 3:
                  display();
                  break;
            case 4:
                  exit(1);    // Exit from the while loop
            default:
                  printf("Invalid option!\n\n");
                  exit(1);   //come out from the while loop
        }
    }
    return 0;
}

void insertdata(int value){   //Inserting the value at the beginning
    struct list*newnode;
    newnode=(struct list*)malloc(sizeof(struct list));
    newnode->data=value;    //assigning the value
    if(head==NULL){
        head=newnode;
        newnode->next=NULL;
    }else{
        newnode->next=head;
        head=newnode;
    }
    printf("Node is added to the list\n\n");
}

void deletedata(){   // Delete at beginning
    struct list*temp=head;
    if(head==NULL){
        printf("List is empty\n");
    }else{
        head=temp->next;
        free(temp);       //deleting the first node in the list
    }
    printf("One node is deleted\n\n");
}

void display(){
    struct list*temp=head;
    printf("The data in the list is: \n");
    while(temp->next!=NULL){
        printf("%d->",temp->data);
        temp=temp->next;
    }
    printf("%d\n\n",temp->data);   //Last node printing
}
Welcome to OnlineGDB Q&A, where you can ask questions related to programming and OnlineGDB IDE and and receive answers from other members of the community.
...