0% found this document useful (0 votes)
6 views11 pages

Data Structure

DSA

Uploaded by

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

Data Structure

DSA

Uploaded by

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

AIM : Write a program to create a single linked list and display the node elements

in reserve order.
Pogram code:

#include<stdio.h>
#include<conio.h>
#include<malloc.h>
#include<stdlib.h>
struct node
{
int info;
struct node *next;
};
struct node *start=NULL;
struct node *create(struct node *start);
struct node *dispaly(struct node *start);
void reverse(struct node *start);
int main()
{
clrscr();
start=create(start);
start=dispaly(start);
printf("\n");
printf("Reverse \t");
reverse(start);
getch ();
return 0;
}
struct node *create(struct node *start)
{
struct node *new_node=NULL,*temp=NULL;
int val;
printf("Enter -1 value to exit list.\n");
printf("Enter the value : \n");
scanf("%d",&val);
while(val!=-1)
{
new_node=(struct node*)malloc(sizeof(struct node));
new_node->info=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 value : \n");
scanf("%d",&val);
}
printf("List is successfully created.\n");
return start;
}
struct node *dispaly(struct node *start)
{
struct node *temp=NULL;
temp=start;
printf("List is :\n");
while(temp!=NULL)
{
printf("%d \t",temp->info);
temp=temp->next;
}
return start;
}
void reverse(struct node *start)
{
struct node *prev=NULL;
struct node *current=start;
struct node *next_node;
while(current!=NULL)
{
next_node=current->next;
current->next=prev;
prev=current;
current=next_node;
}
start=prev;
start=dispaly(start);
}
OUTPUT :

AIM: Write a program to store the elements in 1-D array and perform the
operations like searching, sorting, reversing the elements.
PROGRAM CODE:
#include<stdio.h>
#include<conio.h>
intsize,val;
void disp(int size);
void sort(int size);
void reverse(int size);
void search(intval,int size);
int arr[20];
int main()
{

S.Y.BSC-IT
Int i,ch;
printf("Enter the size of array : ");
scanf("%d",&size);
for(i=0;i<size;i++)
{
scanf("%d",&arr[i]);
}
do
{
printf("\n****Main Menu****\n");
printf("1.Display\n");
printf("2.Sorting\n");
printf("3.Reverse\n");
printf("Enter your Choice : ");
scanf("%d",&ch);
switch(ch)
{
case 1:disp(size);
break;
case 2:sort(size);
break;
case 3:reverse(size);
break;
case 4:printf("Enter value to be search : ");

S.Y.BSC-IT
scanf("%d",&val);
search(val,size);
break;
}
}
while(ch!=4);
getch ();
return 0;
}
void search(intval,int size)
{
inti;
for(i=0;i<size;i++)
{
if(arr[i]==val)
{
printf("Value is found at %d position.",i);
break;
}
}
if(i==size)
{
printf("Value is not found.");
}

S.Y.BSC-IT
}
void disp(int size)
{
inti;
printf("Given Array :\n");
for(i=0;i<size;i++)
{
printf("%d\n",arr[i]);
}
}
void sort(size)
{
inti,j;
for(i=0;i<size;i++)
{
for(j=0;j<size-i-1;j++)
{
if(arr[j]>arr[j+1])
{
int temp;
temp=arr[j];
arr[j]=arr[j+1];
arr[j+1]=temp;
}

S.Y.BSC-IT
}
}
printf("Sorted Array : \n");
for(i=0;i<size;i++)
{
printf("%d \n",arr[i]);
}
}
void reverse(size)
{
inti,j,temp;
j=size-1;
i=0;
while(i<j)
{
temp=arr[i];
arr[i]=arr[j];
arr[j]=temp;
i++;
j--;
}
printf("Reverse order : \n");
for(i=0;i<size;i++)
{

S.Y.BSC-IT
printf("%d \n",arr[i]);
}
}

S.Y.BSC-IT
OUTPUT :

S.Y.BSC-IT

You might also like