Singly Linked List of Integer Data to Create Display and Linear search.

Using Singly linked list of integer data to create and display and linear search , 3rd Sem Data Structure and Application lab 5a program

Singly Linked List (SLL) of Integer Data
a. Create a SLL stack of N integer.
b. Display of SLL
c. Linear search.

/* Singly Linked List (SLL) of Integer Data
a. Create a SLL stack of N integer.
b. Display of SLL
c. Linear search. */

#include <stdio.h>
#include <stdlib.h>
#include<malloc.h>
#include<string.h>
void display();
void stack();;
void insertfront();
void linear();
struct node{
int data;
struct node *next;
};
struct node *first=NULL,*last=NULL,*temp;
int main(){
    stack();

    }

}
void stack(){
    int ch,n,i;
    while(1){
        printf("1.create\n 2.display\n 3.linear search\n 4.exit\n");
        printf("Enter your choice");
        scanf("%d",&ch);
        switch(ch){
            case 1:printf("Enter the number of node\n");
                    scanf("%d",&n);
                    for(i=0;i<n;i++)
                        insertfront();
                    break;
            case 2:display();
                    break;
            case 3:linear();
            break;
            case 4:exit(0);
            break;
            default:printf("Invalid choice\n");
        }
    }
}
void insertfront(){
    struct node *temp;
    int data;
    printf("Enter the data :");
    scanf("%d",&data);
    temp=(struct node*)malloc(sizeof(struct node));
    temp->data=data;
    temp->next=first;
    first=temp;
}
void display(){
    struct node *p;
    p=first;
    if(p==NULL)
    {
        printf("List is empty\n");
        return;
    }
    printf("data of list\n");
    while(p!=NULL){
        printf("%d\t",p->data);
        p=p->next;
    }
}
void linear(){
    struct node *p;
    int item,i=0,flag;
    p=first;
    if(p==NULL){
        printf("List is Empty\n");

    }else{
        printf("\n Enter the node data ");
        scanf("%d",&item);
        while(p!=NULL){
            if(p->data==item){
                printf("Data found at %d\n",i+1);
                flag=0;
                return;
            }else{
                flag=1;

            }i++;
            p=p->next;
        }
        if(flag==1){
            printf("item not found\n");
        }
    }
}

Leave a Reply

Your email address will not be published. Required fields are marked *