Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added C program . #876

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Hacktoberfest-topic.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
Label:hactoberfest-accepted
the hacktoberfest topic is added
118 changes: 118 additions & 0 deletions Programming_Languages/C/linked-list-code.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
#include <stdio.h>
#include <stdlib.h>

typedef struct node
{
int data;
struct node *next;
}node;

node* newnode(struct node* new,int n)
{
struct node* temp;
temp=(struct node*)malloc(sizeof(struct node));
temp->data=n;
temp->next=NULL;
if(new==NULL)
{
new=temp;
}
else
{
while(new->next!=NULL)
{
new=new->next;
}
new->next=temp;
}
}
void printlist(struct node* head)
{
head = head->next;
while (head != NULL) {
printf("%d ", head->data);
if (head->next)
printf("-> ");
head = head->next;
}
printf("\n");
}

// Case 1: Deleting the first element from the linked list
node * deleteFirst(struct node * head){
struct node * ptr = head;
head = head->next;
free(ptr);
return head;
}

// Case 2: Deleting the element at a given index from the linked list
node * deleteAtIndex(struct node * head, int index){
struct node *p = head;
struct node *q = head->next;
for (int i = 0; i < index-1; i++)
{
p = p->next;
q = q->next;
}

p->next = q->next;
free(q);
return head;
}

// Case 3: Deleting the last element
node * deleteLast(struct node * head){
struct node *p = head;
struct node *q = head->next;
while(q->next !=NULL)
{
p = p->next;
q = q->next;
}

p->next = NULL;
free(q);
return head;
}

int main()
{
int a,data,i,j,x;
node* head;
printf("\nEner the size of the linked list: ");
scanf("%d",&a);
printf("\nEnter data in the linked list: ");
for(i=1;i<=a;i++)
{
scanf("%d",&data);
newnode(head,data);
}
printf("\nLinked list before deletion :\n");
printlist(head);
printf("\nChoose out of given below options :\n");
printf("Press '1' to Deleting the first element from the linked list\n");
printf("Press '2' to Deleting the element at a given index from the linked list\n");
printf("Press '3' to Deleting the last element from the linked list\n");
printf("\nEnter the option: ");
scanf("%d",&x);
switch (x)
{
case 1: head = deleteFirst(head);
printf("\nLinked list after deletion:\n");
printlist(head);
break;
case 2: printf("Enter the index position: ");
scanf("%d",&j);
head = deleteAtIndex(head, j);
printf("\nLinked list after deletion:\n");
printlist(head);
break;
case 3: head = deleteLast(head);
printf("\nLinked list after deletion:\n");
printlist(head);
break;
default: printf("\nWrong Input !!!\n");break;
}
return 0;
}