Professional Documents
Culture Documents
ASSIGNMENT-8.Ishika Pal
ASSIGNMENT-8.Ishika Pal
Algorithm:
Create list:
Step 1: create a node
Step 2: temp->info=data
Step 3: temp->link=NULL
Step 4: if start==NULL then go to step 4.1 else go to step 4.2
Step 4.1: start=temp
Step 4.2: q=start
Step 4.3: while(q->link!=NULL)
Step 4: while(temp->link!=NULL)
Step 4.1: q=temp
Step 4.2= temp=temp->link
Step 5: if(temp==start) then go to step 5.1 else go to step 5.2
Step 5.1=start =NULL
Step 6: free(temp)
Delete any position:
Step 1: if(pos==1)
Step 1.1: temp pointer to start
Step 1.2: start->start->link
Step 6: q->link=temp->link
Step 7: free(temp)
Delete w.r.t data:
Step 1: if(start->info==data)
Step 1.1: temp=start
Source code:
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
struct node *link;
}*start;
main()
{
int ch,i,n,m,pos;
while(1)
{
printf("1. Create\n");
printf("2. Display\n");
printf("3. Delete from beginning\n");
printf("4. Delete from last\n");
printf("5. Delete after any node\n");
printf("6. Delete from any node\n");
printf("7. Delete a node w.r.t a value\n");
printf("enter the choice: ");
scanf("%d",&ch);
switch(ch)
{
case 1:
start=NULL;
printf("enter the total number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements: ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
display();
break;
case 3:
del_beg();
break;
case 4:
del_last();
break;
case 5:
printf("enter the position after which element is deleted: ");
scanf("%d",&pos);
del_after(pos);
break;
case 6:
printf("enter the position: ");
scanf("%d",&pos);
del_any(pos);
break;
case 7:
printf("enter the element: ");
scanf("%d",&m);
del_wrt_value(m);
break;
case 8:
exit(1);
default:
printf("wrong choice.");
}
}
}
create_list(int data)
{
struct node *temp, *q;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
display()
{
struct node *q;
if(start==NULL)
{
printf("list is empty\n");
return;
}
q=start;
printf("list is: \n");
while(q!=NULL)
{
printf("%d\n",q->info);
q=q->link;
}
printf("\n");
}
del_beg()
{
if(start==NULL)
{
printf("list is empty.\n");
return;
}
struct node *temp;
temp=start;
start=start->link;
free(temp);
}
del_last()
{
struct node *temp, *q;
if(start==NULL)
printf("list is empty.\n");
else
{
temp=start;
q=start;
while(temp->link!=NULL)
{
q=temp;
temp=temp->link;
}
if(temp==start)
start=NULL;
else
q->link=NULL;
free(temp);
}
}
del_after(int pos)
{
int i=1;
struct node *temp, *q;
q=start;
for(i=1;i<pos;i++)
q=q->link;
temp=q->link;
q->link=temp->link;
free(temp);
return;
}
del_any(int pos)
{
struct node *temp, *q;
if(pos==1)
{
temp=start;
start=start->link;
free(temp);
return;
}
q=start;
int i;
for(i=2;i<pos;i++)
{
q=q->link;
if(q==NULL)
{
printf("there are less than %d elements",pos);
return;
}
}
temp=q->link;
q->link=temp->link;
free(temp);
}
del_wrt_value(int data)
{
struct node *temp, *q;
if(start->info==data)
{
temp=start;
start=start->link;
free(temp);
return;
}
q=start;
while(q->link->link!=NULL)
{
if(q->link->info==data)
{
temp=q->link;
q->link=temp->link;
free(temp);
return;
}
q=q->link;
}
if(q->link->info==data)
{
temp=q->link;
free(temp);
q->link=NULL;
return;
}
printf("element %d not found\n",data);
}
Output:
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 1
enter the total number of nodes: 10
enter the elements: 2
enter the elements: 8
enter the elements: 4
enter the elements: 3
enter the elements: 7
enter the elements: 9
enter the elements: 6
enter the elements: 12
enter the elements: 10
enter the elements: 5
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
2
8
4
3
7
9
6
12
10
5
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 3
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
9
6
12
10
5
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 4
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
9
6
12
10
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 5
enter the position after which element is deleted: 4
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
6
12
10
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 6
enter the position: 6
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
3
7
6
10
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 7
enter the element: 3
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice: 2
list is:
8
4
7
6
10
1. Create
2. Display
3. Delete from beginning
4. Delete from last
5. Delete after any node
6. Delete from any node
7. Delete a node w.r.t a value
enter the choice:
Algorithm:
Create list:
Step 1: create a node
Step 2: temp->info=data
Step 3: temp->link=NULL
Step 4: if start==NULL then go to step 4.1 else go to step 4.2
Step 4.1: start=temp
Step 4.2: q=start
Step 4.3: while(q->link!=NULL)
Step 3: while(q->link!=NULL)
Step 3.1.1: temp=q->link
Step 3.1.2: while(temp!=NULL)
Step 3.1.2.1: if(q->info>temp->info)
Step 3.1.2.1.1: p=q->link
#include<stdio.h>
#include<malloc.h>
struct node
{
int info;
{
case 1:
start=NULL;
printf("enter the total number of nodes: ");
scanf("%d",&n);
for(i=0;i<n;i++)
{
printf("enter the elements: ");
scanf("%d",&m);
create_list(m);
}
break;
case 2:
display();
break;
case 3:
shorted_list();
break;
case 10:
exit(1);
default:
printf("wrong choice.\n");
}
}
}
create_list(int data)
{
struct node *temp, *q;
temp=(struct node *)malloc(sizeof(struct node));
temp->info=data;
temp->link=NULL;
if(start==NULL)
start=temp;
else
{
q=start;
while(q->link!=NULL)
q=q->link;
q->link=temp;
}
}
display()
{
struct node *q;
if(start==NULL)
{
printf("list is empty\n");
return;
}
q=start;
printf("list is: \n");
while(q!=NULL)
{
printf("%d\n",q->info);
q=q->link;
}
printf("\n");
}
shorted_list()
{
struct node *temp, *q;
int p;
q=start;
while(q->link!=NULL)
{
temp=q->link;
while(temp!=NULL)
{
if(q->info>temp->info)
{
p=q->info;
q->info=temp->info;
temp->info=p;
}
temp=temp->link;
}
q=q->link;
}
}
Output:
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 1
enter the total number of nodes: 5
enter the elements: 45
2. Display
3. Shorting list
4. Quit
enter your choice: 2
list is:
45
35
65
55
25
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 3
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: 2
list is:
25
35
45
55
65
1. Create list
2. Display
3. Shorting list
4. Quit
enter your choice: