How to reverse singly Linklist..
Here we write a program of a linklist in which we create some function for insert the element from the starting of the list , insert the elements at the end of the list , delete the element from the starting of the list , delete the elements from the end of the list, display the list of the elements . and the most important reverse function for reverse the linklist.
#include<stdio.h>
#include”stdlib.h“
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
// function for createNode
struct node * createNode()
{
struct node *temp;
temp=(struct node *)malloc(sizeof(struct node));
return temp;
}
// function for insert the at the first
void insert_first()
{
struct node *temp;
temp=createNode();
printf(“Enter the data to be insertedn”);
scanf(“%d”,&temp->data);
temp->next=start;
start=temp;
}
// function for inset the element in the end of the list;
void insert_end()
{
struct node *temp;
temp=createNode();
printf(“Enter the element to be insertedn”);
scanf(“%d”,&temp->data);
if(start==NULL)
{
temp->next=start;
start=temp;
}
else
{
struct node * t;
t=start;
while(t->next!=NULL)
{
t=t->next;
}
temp->next=NULL;
t->next=temp;
}
}
// function for display the elements of the list
void display()
{
struct node* temp;
temp=start;
if(start==NULL)
printf(“List is emptyn”);
else
{
while(temp!=NULL)
{
printf(“%d “,temp->data);
temp=temp->next;
}
}
}
// function for delete the element from the starting
void delete_start()
{
struct node *temp;
if(start==NULL)
printf(“list is emptyn”);
else
{
temp=start;
start=temp->next;
free(temp);
}
}
// function for delete elements from the end of the list
void delete_end()
{
struct node *temp,*t;
if(start==NULL)
printf(“List is emptyn”);
else
{
t=start;
while(t->next->next!=NULL)
{
t=t->next;
}
temp=t->next;
t->next=NULL;
free(temp);
}
}
//function for reverse the linklist
void reverse()
{
struct node *pre,*curr,*nxt;
pre=NULL;
curr=nxt=start;
while(curr!=NULL)
{
nxt=curr->next;
curr->next=pre;
pre=curr;
curr=nxt;
}
start=pre;
}
// Driver program
int main()
{
int ch;
printf(“1. Insert at the starting of the listn”);
printf(“2. insert the element at the end of the listn”);
printf(“3. display the elementsn”);
printf(“4. delete element from the startingn”);
printf(“5. delete elements from the end of the listn”);
printf(“6. reverse the listn”);
printf(“7. Exitn”);
while(1)
{
printf(“Enter your choicen”);
scanf(“%d”,&ch);
switch(ch)
{
case 1: insert_first();
break;
case 2: insert_end();
break;
case 3: display();
break;
case 4: delete_start();
break;
case 5: delete_end();
break;
case 6: reverse();
break;
case 7: exit(0);
default: printf(“Invalid input”);
}
}
return 0;
}
Output:-
1. Insert at the starting of the list
2. insert the element at the end of the list
3. display the elements
4. delete element from the starting
5. delete elements from the end of the list
6. reverse the list
7. Exit
Enter your choice
1
Enter the data to be inserted
12
Enter your choice
1
Enter the data to be inserted
23
Enter your choice
1
Enter the data to be inserted
34
Enter your choice
3
34 23 12 Enter your choice
6
Enter your choice
Recommended Post:
Hackerearth Problems:-
- Very Cool numbers | Hacker earth solution
- Vowel Recognition | Hackerearth practice problem solution
- Birthday party | Hacker earth solution
- Most frequent | hacker earth problem solution
- program to find symetric difference of two sets
- cost of balloons | Hacker earth problem solution
- Chacha o chacha | hacker earth problem solution
- jadu and dna | hacker earth solution
- Bricks game | hacker earth problem
- Anti-Palindrome strings | hacker earth solution
- connected components in the graph | hacker earth data structure
- odd one out || hacker earth problem solution
- Minimum addition | Hackerearth Practice problem
- The magical mountain | Hackerearth Practice problem
- The first overtake | Hackerearth Practice problem
Hackerrank Problems:-
- Playing With Characters | Hackerrank practice problem solution
- Sum and Difference of Two Numbers | hackerrank practice problem solution
- Functions in C | hackerrank practice problem solution
- Pointers in C | hackerrank practice problem solution
- Conditional Statements in C | Hackerrank practice problem solution
- For Loop in C | hackerrank practice problem solution
Data structure:-
- Program to find cycle in the graph
- Implementation of singly link list
- Implementation of queue by using link list
- Algorithm of quick sort
- stack by using link list
- program to find preorder post order and inorder of the binary search tree
- Minimum weight of spanning tree
- Preorder, inorder and post order traversal of the tree
Key points:-
- How to set limit in the floating value in python
- What is boolean data type
- How to print any character without using format specifier
Must check this:-
MCQs:-