Skip to content

Instantly share code, notes, and snippets.

@lex-world
Created March 6, 2022 03:51
Show Gist options
  • Save lex-world/fdf70e84066c7981331ff2ddfb0a24a9 to your computer and use it in GitHub Desktop.
Save lex-world/fdf70e84066c7981331ff2ddfb0a24a9 to your computer and use it in GitHub Desktop.
#include <stdio.h>
#include <stdlib.h>
struct SLL{
int data;
struct SLL *next;
};
struct SLL *head = NULL;
void addBeginning(){
struct SLL *newNode;
int num;
printf("\nEnter the number:");
scanf("%d", &num);
newNode = (struct SLL*) malloc(sizeof(struct SLL));
newNode->data = num;
newNode->next = NULL;
if(head == NULL) head = newNode;
else{
newNode->next = head;
head = newNode;
}
}
void addEnd(){
struct SLL *newNode, *temp;
int num;
printf("\nEnter the number:");
scanf("%d", &num);
newNode = (struct SLL*) malloc(sizeof(struct SLL));
newNode->data = num;
newNode->next = NULL;
if(head == NULL) head = newNode;
else{
temp = head;
while(temp->next != NULL){
temp = temp -> next;
}
temp -> next = newNode;
}
}
void display(){
struct SLL *temp;
if(head == NULL) printf("\nLinked List does not Exist!");
else{
temp = head;
while(temp != NULL){
printf("%d->", temp -> data);
temp = temp -> next;
}
}
}
void main(){
int choice;
printf("\n1.Add at beginning\n2.Display\n3.Add at end\n4.Exit");
while(1){
printf("\nEnter your choice:");
scanf("%d", &choice);
switch(choice){
case 1:
addBeginning();
break;
case 2:
printf("\nLinked List is:\n");
display();
break;
case 3:
addEnd();
break;
case 4:
exit(0);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment