0% found this document useful (0 votes)
301 views

19.2 Sybsc CS SPPU DS Practical Slip Solutions

Sybsc CS SPPU DS practical slip solutions

Uploaded by

vidyaghodke1998
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)
301 views

19.2 Sybsc CS SPPU DS Practical Slip Solutions

Sybsc CS SPPU DS practical slip solutions

Uploaded by

vidyaghodke1998
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/ 5

19.

There are lists where insertion should ensure the ordering of data elements. Since the elements are in
ascending order the search can terminate once equal or greater element is found. Implement a singly
linked list of ordered integers (ascending/descending) with insert, search and display operations.

i. Search, Display operation carries 5 marks each


ii. Validate at appropriate position
iii. Creation of singly linked list in sorted order

#include<stdio.h>

#include<stdlib.h>

#define newnode (struct node *)malloc(sizeof(struct node));

Struct node

Int data;

Struct node *next;

};

Struct node* create(int n)

Struct node *f,*s;

Int I;

F = newnode;

Printf(“\n Enter the data :”);

Scanf(“%d”,&f->data);

S = f;

For(i=2;i<=n;i++)

s->next = newnode;

s = s->next;

printf(“\n Enter the data :”);


scanf(“%d”,&s->data);

s->next=NULL;

return f;

Void display(struct node* f)

Struct node *t;

Printf(“\n Link list is : \n”);

For(t=f; t!=NULL; t=t->next)

Printf(“\t %d”,t->data);

Struct node *insert(struct node *f,int p)

Int cnt=0,I;

Struct node *t,*s;

For(t=f; t!=NULL; t=t->next)

Cnt++;

If(p>=cnt)

Printf(“\n Invalid position”);

Else

S=newnode;
Printf(“\n Enter the data :”);

Scanf(“%d”,&s->data);

If(p==1)

s->next = f;

f=s;

Else

For(i=1,t=f;i<=p-2;i++)

T=t->next;

s->next = t->next;

t->next = s;

Return f;

Void sort(struct node *f)

Struct node *p,*q;

Int temp;

For(p=f;p!=NULL;p=p->next)

For(q=p->next;q!=NULL;q=q->next)

If(p->data > q->data)

{
Temp=p->data;

p->data=q->data;

q->data=temp;

Main()

Int n,p,p1,ch;

Struct node *l;

Do

Printf(“\n\n 1.create link list \n 2. Display \n 3.insert into link list \n 4.sorted order \n
0.exit “);

Printf(“\n \n Enter Your choice”);

Scanf(“%d”,&ch);

Switch(ch)

Case 1 :printf(“\n Enter how manu elements you wnt to enter :”);

Scanf(“%d”,&n);

L=create(n);

Break;

Case 2 : display(l);

Break;

Case 3 :printf(“\n Enter the position to enter data”);

Scanf(“%d”,&p);
L=insert(l,p);

Break;

Case 4:sort(l);

Break;

Case 0 :exit(0);

Default : printf(“\n Invalid choice”);

while(ch!=0);

You might also like