Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no-2300320130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
#include<stdio.h>
#include<stdlib.h>
struct node
{
int info;
struct node*next;
};
struct node*start = NULL;
void create();
void traverse();
void insert_beg();
void insert_last();
void insert_after_specific_position();
void delete_first();
void delete_last();
void delete_specific();
void count_nodes();
void reverse();
int main(){
int ch;
char choice;
while(1)
{
printf("Enter the linked list operation\n");
printf("[Link] list\n");
printf("[Link]\n");
printf("[Link] at begin\n");
printf("[Link] at the last\n");
printf("[Link] after the specific position\n");
printf("[Link] at the first\n");
printf("[Link] at the last\n");
printf("[Link] at the specific position\n");
printf("[Link]\n");
printf("[Link]\n");
printf("[Link]\n");
printf("Enter your choice:");
scanf("%d",&ch);
switch(ch)
{
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 2300320130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
case 1: create();
break;
case 2: traverse();
break;
case 3: insert_beg();
break;
case 4: insert_last();
break;
case 5: insert_after_specific_position();
break;
case 6: delete_first();
break;
case 7:delete_last();
break;
case 8: delete_specific();
break;
case 10: count_nodes();
break;
case 11: reverse();
break;
case 9: exit(1);
default: printf("Entered invalid choice:-");
}
}
return 0;
}
void create(){
char ch;
struct node*temp = (struct node*)malloc(sizeof (struct node*));
printf("Enter the data");
scanf("%d",&temp->info);
temp->next=NULL;
start=temp;
do{
struct node*temp1=(struct node*)malloc(sizeof(struct node*));
printf("Enter the next Data ");
scanf("%d",&temp1->info);
temp->next=temp1;
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 2300320130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
temp=temp1;
printf("do you want to add more");
scanf("%c",&ch);
}while(ch=='y');
temp->next=NULL;
}
void traverse(){
struct node*temp = start;
if(start==NULL)
{
printf("linklist is empty");
}
while(temp->next!=NULL){
printf("%d ",temp->info);
temp=temp->next;
}
printf("%d ",temp->info);
}
void insert_beg(){
struct node*temp=(struct node*)malloc(sizeof (struct node*));
printf("enter data");
scanf("%d",&temp->info);
temp->next = NULL;
if(start==NULL)
{
start = temp;
}
else{
temp->next = start;
start=temp;
}
}
void insert_last(){
struct node*q=start;
struct node*temp=(struct node*)malloc(sizeof (struct node*));
printf("enter data");
scanf("%d",&temp->info);
temp->next=NULL;
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 2300320130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
if(start==NULL)
{
start=temp;
}
else{
while(q->next!=NULL)
{
q=q->next;
}
q->next=temp;
}
}
void insert_after_specific_position(){
struct node*q=start;
struct node*temp=(struct node*)malloc(sizeof (struct node*));
int x;
printf("enter node");
scanf("%d",&temp->info);
printf("enter node after which do you want to insert");
scanf("%d",&x);
while(q!=NULL && q->info!=x){
q=q->next;
}
if(q==NULL){
printf("node with value %d not found\n",x);
free(temp);
return;
}
temp->next=q->next;
q->next=temp;
}
void delete_first(){
struct node*temp=start;
if(start==NULL){
printf("list is empty");
}
else{
start=start->next;
free(temp);
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 23003120130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
}
}
void delete_last(){
struct node*q=start;
if(start==NULL)
{
printf("list is empty");
}
else{
while(q->next->next!=NULL)
{
q=q->next;
}
free(q->next);
q->next=NULL;
}
}
void delete_specific(){
struct node*q=start;
struct node*temp;
int x;
printf("Enter Which do you want to delete");
scanf("%d",&x);
while(q->next->info!=x){
q=q->next;
}
temp=q->next;
q->next=q->next->next;
free(temp);
}
void count_nodes()
{
int count=0;
struct node*temp=start;
if(start==NULL)
{
printf("list is empty");
}
else{
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 2300320130204
Problem statement – Write a program for linear linkedlist implementation
Code :-
while(temp!=NULL)
{
count++;
temp=temp->next;
}
}
printf("total nodes=%d",count);
}
void reverse()
{
struct node *p, *q, *r;
p=NULL;
q=start;
r=NULL;
while(q!=NULL)
{
r=q->next;
q->next=p;
p=q;
q=r;
}
start=p;
}
Time complexity :- O(n)
Space complexity :- O(n)
Program 6(a)
Program Name – Linear linked list Name - Rohan Kumar
Domain – Linked list Roll no- 2300320130204
Problem statement – Write a program for linear linkedlist implementation
Output :-