#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
typedef struct node{
int data;
struct node *next;
}nptr;
nptr *start=NULL;
void createNode(){
if(start==NULL){
nptr *nn;
nn=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&nn->data);
nn->next=NULL;
start=nn;
printf("\nLink Created\n");
}else{
printf("\nLink Exists\n");
}
}
void viewList(){
nptr *temp=start;
while(temp!=NULL){
printf(" %d ",temp->data);
if(temp->next!=NULL)
printf("->");
temp=temp->next;
}
}
void addAtEnd(){
nptr *nn,*temp=start;
nn=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&nn->data);
nn->next=NULL;
while(temp->next!=NULL)
temp=temp->next;
temp->next=nn;
printf("\nAdded.");
}
void addAtBeginning(){
nptr *nn;
nn=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&nn->data);
nn->next=start;
start=nn;
printf("\nAdded.");
}
void addInBetween(){
nptr *nn,*temp=start;
int ch;
nn=(nptr *)malloc(sizeof(nptr));
printf("\nEnter Data\n");
scanf("%d",&nn->data);
printf("\na. Position wise\nb. Data wise\n");
if(getch()=='a'||getch()=='A'){
printf("\nEnter Position after which node will be inserted\n");
scanf("%d",&ch);
for(int i=0;i<ch-1;i++)
temp=temp->next;
}else{
printf("\nEnter Data after which node will be inserted\n");
scanf("%d",&ch);
while(temp->data!=ch){
temp=temp->next;
}
}
nn->next=temp->next;
temp->next=nn;
printf("\nAdded.");
}
void deleteFromEnd(){
nptr *temp=start;
while(temp->next->next!=NULL){
temp=temp->next;
}
free(temp->next);
temp->next=NULL;
printf("\nDeleted.");
}
void deleteFromBeginning(){
nptr *temp=start;
start=start->next;
free(temp);
printf("\nDeleted.");
}
void deleteFromBetween(){
nptr *tmp,*temp=start;
int ch;
printf("\na. Position wise\nb. Data wise\n");
if(getch()=='a'||getch()=='A'){
printf("\nEnter Position of node to be deleted\n");
scanf("%d",&ch);
for(int i=1;i<ch-1;i++)
temp=temp->next;
}else{
printf("\nEnter Data of node to be deleted\n");
scanf("%d",&ch);
while(temp->next->data!=ch){
temp=temp->next;
}
}
tmp=temp->next;
temp->next=temp->next->next;
free(tmp);
printf("\nDeleted.");
}
void sortList(){
nptr *temp=start,*tmp;
int m;
for(;temp!=NULL;temp=temp->next){
for(tmp=temp->next;tmp!=NULL;tmp=tmp->next){
if(temp->data>tmp->data){
m=tmp->data;
tmp->data=temp->data;
temp->data=m;
}
}
}
printf("\nSorted");
}
void main()
{
int ch;
printf("\nLINK LIST\n");
while(1){
printf("\n\nEnter Choice\n\n0 Exit\n1 Create \n2 Add at End\n3 Add at beginning\n4 Add in between\n5 Delete from End\n6 Delete from beginning\n7 Delete from between\n8 Display List\n9 Sort\n\n");
ch=getch();
switch(ch){
case '1':createNode();break;
case '2':addAtEnd();break;
case '3':addAtBeginning();break;
case '4':addInBetween();break;
case '5':deleteFromEnd();break;
case '6':deleteFromBeginning();break;
case '7':deleteFromBetween();break;
case '8':viewList();break;
case '9':sortList();break;
case '0':exit(0);
default: printf("\nInvalid Choice\n");
}
}
}
Simple Link List in C
Be the first to comment
You can use [html][/html], [css][/css], [php][/php] and more to embed the code. Urls are automatically hyperlinked. Line breaks and paragraphs are automatically generated.