NotesWhat is notes.io?

Notes brand slogan

Notes - notes.io

#include<stdio.h>
#include<stdlib.h>
int len;
void create(void);
void append(void);
void addatbegin(void);
void addatafter(void);
int length(void);
void display(void);
void delete(void);
void deletebykey(void);
void reverse(void);
void sum(void);
void merge(void);
struct node
{
int data;
struct node* link;
};
struct node* root=NULL;
struct node* head=NULL;
void main()
{
int ch;
while(1)
{
printf("single linked list operations nn");
printf("1. create a node n");
printf("2. append n");
printf("3. add at begin n");
printf("4. add at after n");
printf("5. length n");
printf("6. display n");
printf("7. delete by locationn");
printf("8. delete by key n");
printf("9. reverse the listn");
printf("10. sum of the elementsn");
printf("11. exitnn");
printf("enter your choice n");
scanf("%d",&ch);
switch(ch)
{
case 1 : create();
break;
case 2 : append();
break;
case 3 : addatbegin();
break;
case 4 : addatafter();
break;
case 5 : len = length();
printf("length : %d n",len);
break;
case 6 : display();
break;
case 7 : delete();
break;
case 8 : deletebykey();
break;
case 9 : reverse();
break;
case 10 : sum();
break;
case 11: exit(1);
default : printf("invalid input: n");
}
}
}

void create()
{
struct node* new;
new=(struct node*)malloc(sizeof(struct node));
printf("enter node data");
scanf("%d",&new->data);
new->link=NULL;
if(root==NULL)
{
root=new;
}
}


void append()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
struct node* p;
p=root;
while(p->link!=NULL)
{
p=p->link;
}
p->link=temp;
}
}

int length()
{
int count = 0;
struct node* temp;
temp=root;
while(temp!=NULL)
{
count++;
temp=temp->link;
}
len=count;
return len;
}

void display()
{
struct node* temp;
temp=root;
if(temp==NULL)
{
printf("no nodes found, list is empty");
}
else
{
while(temp!=NULL)
{
printf("%d-->",temp->data);
temp=temp->link;
}
printf("nn");
}
}

void addatbegin()
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data");
scanf("%d",&temp->data);
temp->link=NULL;
if(root==NULL)
{
root=temp;
}
else
{
temp->link=root; //right side connection
root=temp; //left side connection
}
}

void delete()
{
struct node* temp;
int loc;
printf("enter loaction to delete");
scanf("%d",&loc);
if(loc>length())
{
printf("invalid choice.");
}
else if(loc==1)
{
temp=root;
root=temp->link;
temp->link=NULL;
free(temp);
}
else
{
struct node* p=root, *q;
int i=1;
while(i<loc-1) //pointer p stops at 1 node less than the loacation
{
p=p->link;
i++;
}
q=p->link;
p->link=q->link;
q->link=NULL;
free(q);
}
}

void addatafter()
{
struct node* temp, *p;
int loc, len, i=1;
printf("enter location: n");
scanf("%d",&loc);
len=length();
if(loc>len)
{
printf("invalid input n");
printf("currently list has %d nodes",len);
}
else
{
p=root;
while(i<loc) //pointer p stops at location
{
p=p->link;
i++;
}
}
temp=(struct node*)malloc(sizeof(struct node));
printf("enter node data:");
scanf("%d",&temp->data);
temp->link=NULL;
temp->link=p->link; //right side connection
p->link=temp; //left side connection
}

void deletebykey()
{
struct node* temp, *p, *q;
int key;
temp=root;
if(temp==NULL)
{
printf("empty list");
}
else
{
printf("enter the key value : n");
scanf("%d",&key);
if((temp->data==key) && (temp->link==NULL))
{
p=temp;
root=NULL;
free(p);
}
p=temp;
while((p->link!=NULL) && (p->data!=key))
{
q=p;
p=p->link;
}
if(p->data==key)
{
q->link=p->link;
free(p);
}
else
{
printf("operation not possible n");
}
}
}

void reverse()
{
struct node* temp, *ptr, *curr, *prev;
temp=root;
if(temp==NULL)
{
printf("linked list is emptynn");
return;
}
else
{
curr=temp;
prev=curr->link;
curr->link=NULL;
while(prev->link!=NULL)
{
ptr=prev->link;
prev->link=curr;
curr=prev;
prev=ptr;
}
prev->link=curr;
root=prev;
}
}

void sum()
{
struct node* temp;
int q, sum=0;
temp=root;
if(temp==NULL)
{
printf("linked list is emptynn");
return;
}
else
{
while(temp!=NULL)
{
q=temp->data;
sum=sum+q;
temp=temp->link;
}
printf("the sum of all the elements is %d ",sum);
printf("nn");
}
}








     
 
what is notes.io
 

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

     
 
Shortened Note Link
 
 
Looding Image
 
     
 
Long File
 
 

For written notes was greater than 18KB Unable to shorten.

To be smaller than 18KB, please organize your notes, or sign in.