Commit 496fb3db authored by nouman.ashraf's avatar nouman.ashraf

Initial commit

parents
#include <stdio.h>
#include <stdlib.h>
struct node {
int data;
struct node *next_Link;
};
struct node *head = NULL;
void insert_at_start(int);
void insert_at_end(int);
void traverse();
void delete_from_start();
void delete_from_end();
void insert_pos();
void display();
void sorting();
int main () {
int i, data;
for (int x=0; x<100; x++) {
printf("1. Insert an element at the beginning of linked list.\n");
printf("2. Insert an element at the end of linked list.\n");
printf("3. Traverse linked list.\n");
printf("4. Delete an element from beginning.\n");
printf("5. Delete an element from end.\n");
printf("6. Insert an element at any position.\n");
printf("7. Display\n");
printf("8. Sorting\n");
printf("9. Exit\n");
printf("Enter Number");
scanf("%d", &i);
if (i == 1) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_start(data);
}
else if (i == 2) {
printf("Enter value of element\n");
scanf("%d", &data);
insert_at_end(data);
}
else if (i == 3)
traverse();
else if (i == 4)
delete_from_start();
else if (i == 5)
delete_from_end();
else if (i == 6)
insert_pos();
else if (i == 7)
display();
else if (i == 8)
sorting();
else if (i == 9)
break;
else
printf("Please enter valid input.\n");
}
return 0;
}
void insert_at_start(int x) {
struct node *t;
t = (struct node*)malloc(sizeof(struct node));
t->data = x;
if (head == NULL) {
head = t;
head->next_Link = NULL;
return;
}
t->next_Link = head;
head = t;
}
void insert_at_end(int x) {
struct node *t, *temp;
t = (struct node*)malloc(sizeof(struct node));
t->data = x;
if (head == NULL) {
head = t;
head->next_Link = NULL;
return;
}
temp = head;
while (temp->next_Link != NULL)
temp = temp->next_Link;
temp->next_Link = t;
t->next_Link = NULL;
}
void traverse() {
struct node *p1, *p2;
int temp;
p1=head;
while (p1->next_Link!=NULL)
{
p2 = p1->next_Link;
while (p2!=NULL)
{
if (p1->data < p2->data)
{
temp = p1->data;
p1->data = p2->data;
p2->data = temp;
}
p2 = p2->next_Link;
}
p1 = p1->next_Link;
}
}
void display()
{
struct node *temp;
temp=head;
while(temp!=NULL)
{
printf("%d\n",temp->data);
temp=temp->next_Link;
//printf("%d\n", temp->data);
}
}
void sorting()
{
struct node *p1, *p2;
int temp;
p1=head;
while (p1->next_Link!=NULL)
{
p2 = p1->next_Link;
while (p2!=NULL)
{
if (p1->data > p2->data)
{
temp = p1->data;
p1->data = p2->data;
p2->data = temp;
}
p2 = p2->next_Link;
}
p1 = p1->next_Link;
}
}
void insert_pos()
{
struct node *p,*temp;
int i,pos;
temp=(struct node *)malloc(sizeof(struct node));
if(temp==NULL)
{
printf("\nOut of Memory Space:n");
return;
}
printf("Enter the position for the new node to be inserted:");
scanf("%d",&pos);
printf("Enter the data value of the node: ");
scanf("%d",&temp->data) ;
temp->next_Link=NULL;
if(pos==0)
{
temp->next_Link=head;
head=temp;
}
else
{
for(i=0,p=head;i<pos-1;i++) { p=p->next_Link;
if(p==NULL)
{
printf("nPosition not found:[Handle with care]n");
return;
}
}
temp->next_Link =p->next_Link ;
p->next_Link=temp;
}
}
void delete_from_start() {
struct node *t;
int n;
if (head == NULL) {
printf("Linked list is empty.\n");
return;
}
n = head->data;
t = head->next_Link;
free(head);
head = t;
printf("%d deleted from the beginning successfully.\n", n);
}
void delete_from_end() {
struct node *t, *u;
int n;
if (head == NULL) {
printf("Linked list is empty.\n");
return;
}
if (head->next_Link == NULL) {
n = head->data;
free(head);
head = NULL;
printf("%d deleted from end successfully.\n", n);
return;
}
t = head;
while (t->next_Link != NULL) {
u = t;
t = t->next_Link;
}
n = t->data;
u->next_Link = NULL;
free(t);
printf("%d deleted from end successfully.\n", n);
}
\ No newline at end of file
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment