0% found this document useful (0 votes)
4 views3 pages

Practical 2A

The document contains a C program that implements a singly linked list with functionalities to create, display, and reverse the list. The main menu allows users to choose operations on the linked list, and the reverse function modifies the list to display its elements in reverse order. The program utilizes structures and dynamic memory allocation to manage the linked list nodes.

Uploaded by

fannyskylark2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views3 pages

Practical 2A

The document contains a C program that implements a singly linked list with functionalities to create, display, and reverse the list. The main menu allows users to choose operations on the linked list, and the reverse function modifies the list to display its elements in reverse order. The program utilizes structures and dynamic memory allocation to manage the linked list nodes.

Uploaded by

fannyskylark2003
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 3

Q.2A.

Write a program to create a single linked list and display


the node elements in reverse order.
#include<stdio.h>
#include<malloc.h>
struct node
{
int data;
struct node *next;
};
struct node *start=NULL;
struct node *create(struct node*);
struct node *display(struct node*);
void reverse(struct node*);
struct node *deletell(struct node*);
int main()
{
int opt;
clrscr();
do
{
printf("\n*****MAIN MENU******\n");
printf("1.Create a list\n");
printf("2.Display list\n");
printf("3.Reverse\n");
printf("4.EXIT\n");
printf("Enter your choice: \n");
scanf("%d",&opt);
printf("\n");
switch(opt)
{
case 1: start=create(start);
break;
case 2: start=display(start);
break;
case 3: reverse(start);
break;
case 4: break;
}
}while(opt!=4);
return 0;
}
void reverse(struct node*head)
{
struct node* prev =NULL;
struct node* current =head;
struct node* next;
while(current!=NULL)
{
next =current->next;
current->next = prev;
prev = current;
current = next;
}
head = prev;
head=display(head);
}
struct node*create(struct node *start)
{
struct node *new_node=NULL,*temp=NULL;
int val;
printf("Enter the data or enter -1 to exit:\n");
scanf("%d",&val);
while(val!=-1)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node->data=val;
if(start==NULL)
{
start=new_node;
new_node->next=NULL;
}
else
{
temp=start;
while(temp->next!=NULL)
{
temp=temp->next;
}
temp->next=new_node;
new_node->next=NULL;
}
printf("Enter the data or enter -1 to exit:\n");
scanf("%d",&val);
}
printf("Linked List successfully craeted.\n");
return start;
}
struct node*display(struct node *start)
{
struct node *temp=NULL;
temp=start;
printf("The Linked list is:\n");
while(temp!=NULL)
{
printf("\t%d",temp->data);
temp=temp->next;
}
printf("\n");
return start;
}
OUTPUT:

You might also like