ASSIGNMENT 7 CS 392 : TCPTUTOR

7
113

PROBLEM STATEMENT

Write a Menu Driven C program to implement the following after creating a Single Linked List (SLL)

a)Insert a node (insert_at_beg/insert_at_end/insert_at_pos) in SLL

b)Delete a node (del_from_beg/del_from_end/del_at_pos) in SLL

c)Traverse the nodes of the list.

ALGORITHM

ALGORITHM FOR INSERTION
Insert at beginning
Step 1: If AVAIL=NULL
Write “OVERFLOW”
GOTO END
Step 2: Set NEW_NODE=AVAIL
Step 3: NEW_NODE->DATA=VAL
Step 4: NEW_NODE->NEXT=START
Step 5: START=NEW_NODE
Step 6: END

Insert at end
Step 1: If AVAIL=NULL
Write “OVERFLOW”
GOTO END
Step 2: Set NEW_NODE=AVAIL
Step 3: NEW_NODE->DATA=VAL
Step 4: Set NEW_NODE->NEXT=NULL
Step 5: Set PTR=START
Step 6: Repeat Step 7 while PTR->NEXT!=NULL
Step 7: Set PTR=PTR->NEXT
[End of loop]
Step 8: Set PTR->NEXT=NEW_NODE
Step 9: END

Insert at position(insert before any given value NUM)
Step 1: If AVAIL=NULL
Write “OVERFLOW”
GOTO END
Step 2: Set temp=avail
Step 3: set temp->link=NULL
Step 4: Set temp->data=value
Step 5: set i=1
Step 6: repeat step while I<pos-1 AND t!=NULL

set t=t->link

[End of loop]
Step 7: temp->link=t->link

Step 8: t->link=temp
Step 9: exit

ALGORITHM FOR DELETION
Delete beginning node
Step 1: If START=NULL
Write “UNDERFLOW”
Goto END
[End of If]
Step 2: Set PTR=START
Step 3: Set START=START->NEXT
Step 4: Free PTR
Step 5: END

Delete last node
Step 1: If START=NULL
Write “UNDERFLOW”
Goto END
[End of If]
Step 2: If START->NEXT=NULL
Set PTR=START and START=NULL
Set TEMP=PTR->DATA
Step 3: Else PREPTR=START and PTR=START->NEXT
Step 4: Repeat Steps 5 and 6 while(PTR->NEXT!=NULL)
Step 5: Set PREPTR=PTR
Step 6: Set PTR=PTR->NEXT
[end of while]
Step 7: Set PREPTR->NEXT=NULL
Step 8: TEMP=PTR->DATA
[end of else]
Step 9: FREE PTR
Step 10: END

Delete node at a position
Step 1: Step 1: If START=NULL
Write “UNDERFLOW”
Goto END
[End of If]
Step 2: Set PTR=START
Step 3: If INDEX=1
Set START=PTR->NEXT
FREE PTR
[End of If]
Step 4: Else Set i=1
Repeat Steps 5 & 6 while i<=INDEX-1
Step 5: PTR=PTR->NEXT
Step 6: Set i=i+1
Step 7: Set PREPTR=PTR->NEXT
Step 8: Set PTR->NEXT=PREPTR->NEXT
Step 9: FREE PREPTR
[End of Else]
Step 10:END

ALGORITHM FOR TRAVERSING
Display elements of the list
Step 1: If START=NULL
Write “UNDERFLOW”
Goto END
[End of If]
Step 2: Set TEMP=START
Step 3: Repeat Steps 4 and 5 while(TEMP!=NULL)
Step 4: Print TEMP->DATA (i.e., data of current node)
Step 5: TEMP=TEMP->NEXT
Step 6: END

 

SOURCE CODE

#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{ int data;
struct node *link;
};
struct node *start=NULL;
struct node* createnode()
{ struct node *n;
n=(struct node *)malloc(sizeof(struct node));
return n;
}
int insertbeg()
{ struct node *temp;
temp=createnode();
printf(“Enter the data”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(start==NULL)
start=temp;
else
{ temp->link=start;
start=temp;
}
return 0;
}
int insertend()
{ struct node *temp,*t;
temp=createnode();
printf(“Enter the data”);
scanf(“%d”,&temp->data);
temp->link=NULL;
if(start==NULL)
start=temp;
else
{ t=start;
while(t->link!=NULL)
t=t->link;
t->link=temp;
}
return 0;
}
int insertpos()
{ int pos,i;
struct node *temp,*t;
temp=createnode();
printf(“Enter the data”);
scanf(“%d”,&temp->data);
temp->link=NULL;
printf(“Enter the position”);
scanf(“%d”,&pos);
if(pos==1)
{ temp->link=start;
start=temp;
}
else
{
t=start;
for(i=1;i<pos-1&&t!=NULL;i++)
t=t->link;
temp->link=t->link;
t->link=temp;
}
return 0;
}
int display()
{ struct node *t;
t=start;
if(start==NULL)
printf(“list is empty”);
else
{ printf(“List:”);
while(t!=NULL)
{ printf(“%d,”,t->data);
t=t->link;
}
}
return 0;
}
int delbeg()
{ struct node *t;
t=start;
if(start==NULL)
printf(“Underflow”);
else
{ printf(“Deleted value is:%d”,t->data);
start=start->link;
free(t);
}
return 0;
}
int delend()
{ struct node *t,*ptr;
t=start;
if(start==NULL)
printf(“Underflow”);
else
{ while(t->link!=NULL)
{ ptr=t;
t=t->link;
}
printf(“Deleted value:%d”,t->data);
ptr->link=NULL;
free(t);
}
return 0;
}
int delpos()
{ struct node *t,*ptr;
int pos,i;
if(start==NULL)
{ printf(“Underflow”);
}
else
{ printf(“Enter the position to delete”);
scanf(“%d”,&pos);
t=start;
for(i=1;i<pos&&t!=NULL;i++)
{ ptr=t;
t=t->link;
}
ptr->link=t->link;
printf(“Deleted value is:%d”,t->data);
free(t);
}
return 0;
}
void main()
{ int choice,ch;
while(1)
{ printf(“\n1. Insert\n”);
printf(“2. Delete\n”);
printf(“3. Display\n”);
printf(“4. Exit\n”);
printf(“Enter your choice”);
scanf(“%d”,&choice);
clrscr();
switch(choice)
{ case 1:
printf(“1. Insert at begning\n”);
printf(“2. Insert at end\n”);
printf(“3. insert at any posistion\n”);
printf(“Enter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{ case 1:
insertbeg();
break;
case 2:
insertend();
break;
case 3:
insertpos();
break;
default:
printf(“Enter correct choice”);
}
break;
case 2:
printf(“1. Delete at begning\n”);
printf(“2. Delete at end\n”);
printf(“3. delete at any posistion\n”);
printf(“Enter your choice:”);
scanf(“%d”,&ch);
switch(ch)
{ case 1:
delbeg();
break;
case 2:
delend();
break;
case 3:
delpos();
break;
default:
printf(“Enter correct choice”);
}
break;
case 3:
display();
break;
case 4:
exit(0);
break;
default:
printf(“Enter corrcet chice”);
}
}
}

OUTPUT

1. Insert
2. Delete
3. Display
4. Exit
Enter your choice:1
1. Insert at beginning
2. Insert at end
3. Insert at any position
Enter your choice:1
Enter a number3

<MENU>
Enter your choice:1
1. Insert at beginning
2. Insert at end
3. Insert at any position
Enter your choice:1
Enter a number3

<MENU>
Enter your choice:1
1. Insert at beginning
2. Insert at end
3. Insert at any position
Enter your choice:3
Enter the position:2
Enter a number5

<MENU>
Enter your choice:3
List:3,5,3,
<MENU>
Enter your choice:2
1. Delete at begning
2. Delete at end
3. Delete at any position
Enter your choice:2
Deleted value is:3
<MENU>
Enter your choice:3
List:3,5,
<MENU>

************************

7 COMMENTS

  1. NEAUVIA,歐洲血統透明質酸,目前遍布於全世界56個國家,行政總部在瑞士,卻是源自於意大利的品牌。採用21世紀先進的技術,提取出前所未有的高純度透明質酸,迅速的在全球攻占市場,卓越的品質,領先的技術和平民的價位,已經逐漸的出現在大眾的視線裡,被越來越的人使用。NEAUVIA以系列分女性專用和男性專用,大中小分子以及唇部專用和私處專用,其中私處專用己經在國內一些比較大的整形機構普遍使用。素材提取無與倫比的純淨,運用嶄新PEG鏈結技術-更安全

  2. 前額整形、額頭整形、自體脂肪、玻尿酸、膠原蛋白、雅德美AQUAMID、額骨柔化等一些相關議題、似是而非或有爭議的事項的披露與討論。

LEAVE A REPLY

Please enter your comment!
Please enter your name here