Notes
Notes - notes.io |
#include <stdlib.h>
#include <stddef.h>
struct student
{
char name[10];
int roll;
float per;
struct student * next;
}; // step1 creating a structure
typedef struct student node; //typedef as node and declaring functions
node* create_list();
void display(node *);
void insert(node ** head);
int count=0;
void insertAtBegining(node** headp); //insert and delete needs double pointer
void insertAtEnd(node ** headp);
void insertInbetween(node **headp);
void deletenode(node ** headp);
void deleteAtBegining(node **headp);
void deleteAtEnd(node ** headp);
void deleteInBetween(node ** headp);
void size();
int main(){
node * head;
int selection; //needs a node pointer
head=create_list();
printf(".............Linked List created with size=%dn",count); // assign value via create function
printf("tt******Enter your choicen"
" " 1" to display the linked Listn"
" "2" to insert a noden"
" "3" to delete a noden"
" "4" size of linkedlistn"
" "5" to quitn");
scanf("%d",&selection);
while (selection != 5)
{
switch (selection){
case 1:
display(head);
break;
case 2:
insert(&head);
break;
case 3:
deletenode(&head);
break;
case 4:
size();
break;
default:
printf("Invalid Entry");
break;
}
printf("tt********Enter your choicen"
" "1" to display the linked Listn"
" "2" to insert a noden"
" "3" to delete a noden"
" "4" size of linkedlistn"
" "5" to quitn");
scanf("%d",&selection);
}
return 0;
}
node * create_list(){
node * p,*head;
int n;
printf("Enter the number of nodes neededn"); //entering input from the user and looping
scanf("%d",&n);
for(int k=0;k<n;k++){
printf("ENter the name,roll and percentagen");
if (k==0)
{
head=(node *) malloc(sizeof(node)); //allocating dynamic memory
p=head; //copimg to p
}else{
p->next=(node *)malloc(sizeof(node)); //allocating next node dynalically
p=p->next; //initialising p->next
}
scanf("%s%d%f",p->name,&p->roll,&p->per);
}
p->next=NULL;
count=n;
return head;
}
void display(node * head){
node * p=head;
while(p!=NULL){
printf("Name is %s roll is %d and percentage is %fn",p->name,p->roll,p->per);
p=p->next;
}
}
void insert(node **headp){
int sel;
printf("tt ****Enter your choice n"
" " 1" you have to insert at the beginningn"
" "2" you have to insert at the endn"
" " 3" to insert in betweenn");
scanf("%d",&sel);
switch (sel){
case 1:
insertAtBegining(headp);
break;
case 2:
insertAtEnd(headp);
break;
case 3:
insertInbetween(headp);
break;
default :
printf("invalid Entry");
break;
}
count++;
}
void insertAtBegining(node **headp){
node * new1;
new1=(node *) malloc(sizeof(node));
printf ("enter the name roll and percentagen");
scanf("%s%d%f",new1->name,&new1->roll,&new1->per);
new1->next=(*headp);
*headp=new1;
}
void insertAtEnd(node ** headp){
node * pnode=*headp;
node * new2,*tailnode;
tailnode=*headp;
printf ("enter the name roll and percentagen");
scanf("%s%d%f",new2->name,&new2->roll,&new2->per);
while(tailnode->next!= NULL)
{
tailnode=tailnode->next;
}
new2->next=NULL;
tailnode->next=new2;
}
void insertInbetween(node ** headp){
node * new3,*refnode,*nextnode,*prenode;
// prenode= *headp;
nextnode=*headp;
// nextnode=prenode->next;
int refno;
printf("Enter before which roll number we have to add the new noden");
scanf("%d",&refno);
new3=(node *) malloc(sizeof(node));
while(nextnode!=NULL){
if(nextnode->roll == refno)
{ refnode=nextnode;
break;}
else{
refnode=NULL;
// printf("%d roll number does not exitn",refno);
}
prenode=nextnode;
nextnode=prenode->next;
}
printf ("Enter the name,roll and percentage of the new noden");
scanf("%s%d%f",new3->name,&new3->roll,&new3->per);
new3->next=refnode;
prenode->next=new3;
}
void deletenode(node ** headp){
printf("tt ********Enter choice *******n"
" "1" To delete the first noden"
" "2" To delete the end noden"
" "3" To delete an in between noden");
int sel;
scanf("%d",&sel);
switch(sel){
case 1:
deleteAtBegining(headp);
break;
case 2:
deleteAtEnd(headp);
break;
case 3:
deleteInBetween(headp);
break;
defaut:
printf("Invalid entryn");
break;
}
count--;
}
void deleteAtBegining(node ** headp){
node * temp =*headp;
*headp=(*headp)->next;
free(temp);
}
void deleteAtEnd(node ** headp){
node * tail =*headp;
node * pretail;
while(tail->next != NULL){
pretail=tail;
tail=tail->next;
}
pretail->next=NULL;
free(tail);
}
void deleteInBetween(node ** headp){
printf("Enter the roll no of the node to be deletedn" );
int delroll;
scanf("%d",&delroll);
node *temp=*headp;
node * previous;
while(temp->roll != delroll)
{
previous=temp;
temp=temp->next;
}
previous->next=temp->next;
free(temp);
}
void size(){
printf("The current size of linkedlist is %dn",count);
}
|
Notes.io is a web-based application for taking notes. You can take your notes and share with others people. If you like taking long notes, notes.io is designed for you. To date, over 8,000,000,000 notes created and continuing...
With notes.io;
- * You can take a note from anywhere and any device with internet connection.
- * You can share the notes in social platforms (YouTube, Facebook, Twitter, instagram etc.).
- * You can quickly share your contents without website, blog and e-mail.
- * You don't need to create any Account to share a note. As you wish you can use quick, easy and best shortened notes with sms, websites, e-mail, or messaging services (WhatsApp, iMessage, Telegram, Signal).
- * Notes.io has fabulous infrastructure design for a short link and allows you to share the note as an easy and understandable link.
Fast: Notes.io is built for speed and performance. You can take a notes quickly and browse your archive.
Easy: Notes.io doesn’t require installation. Just write and share note!
Short: Notes.io’s url just 8 character. You’ll get shorten link of your note when you want to share. (Ex: notes.io/q )
Free: Notes.io works for 12 years and has been free since the day it was started.
You immediately create your first note and start sharing with the ones you wish. If you want to contact us, you can use the following communication channels;
Email: [email protected]
Twitter: http://twitter.com/notesio
Instagram: http://instagram.com/notes.io
Facebook: http://facebook.com/notesio
Regards;
Notes.io Team