Commit 7b899596 authored by nouman.ashraf's avatar nouman.ashraf

Initial commit

parents
File added
#include <stdio.h>
#include <stdlib.h>
typedef struct node
{
int x;
struct node* n;
} node;
typedef void (*callback)(node* x);
node* create(int x,node* n)
{
node* insertnode = (node*)malloc(sizeof(node));
if(insertnode == NULL)
{
printf("Error with node.\n");
exit(0);
}
insertnode->x = x;
insertnode->n = n;
return insertnode;
}
node* beginning(node* head, int x) //In this function, the head pointer needs to point to the next node.
{
node* insertnode = create(x,head); // This is creating the next node
head = insertnode; // The variable head is being assigned to insertnode
return head; // The program will return head.
}
node* insertNodeAfter(node *head, int x, node* idnode)
{
if(head == NULL || idnode == NULL) // The if statement is comparing if both pointers equal null. If they do equal null, the program will return null.
return NULL;
node *pointer = head;
while(pointer != idnode)
pointer = pointer->n;
if(pointer != NULL)
{
node* insertnode = create(x,pointer->n);
pointer->n = insertnode;
return head;
}
else
{
return NULL;
}
}
node* removefirstNode(node* head)
{
if(head == NULL)
return NULL;
node *first = head;
head = head->n;
first->n = NULL;
if(first == head)
head = NULL;
free(first);
return head;
}
node* removeNode(node* head, node* hold)
{
if(hold == NULL)
return NULL;
if(hold->n == NULL)
return removefirstNode(head);
node* point = head;
while(point != NULL)
{
if(point->n == hold)
break;
point = point->n;
}
if(point != NULL)
{
node* spare = point->n;
point->n = spare->n;
spare->n = NULL;
free(spare);
}
return head;
}
void display(node* dis)
{
if(dis != NULL)
printf("%d ",dis->x);
}
node* find(node* head, int x)
{
node *point = head;
while(point!=NULL)
{
if(point->x == x)
{
return point;
}
point = point->n;
}
return NULL;
}
void printlist(node * head) {
node * now = head;
while(now != NULL) {
printf("%d\n", now->x);
now = now->n;
}
}
void traverse(node* head,callback a)
{
node* locate = head;
while(locate != NULL)
{
a(locate);
locate = locate->n;
}
}
node* sortList(node* head)
{
node *a, *b, *c;
a = head;
head = NULL;
while(a != NULL) // if a is equal to nothing, node c will equal to a and a will point to itself and the node n.
{
c = a;
a = a->n;
if (head != NULL)
{
if(c->x > head->x) //x is the integer variable for the node structure.If c point to x is less than head pointing to head, b gets assgined to head.
{
b = head;
while ((b->n != NULL && c->x > b->n->x)) // In the while loop, while b point to n is not equal to null and if c point to x is greater than b pointing to n pointing to x
{
b = b->n; // b will point to n to compare the values
}
c->n = b->n; // c pointing to n would equal b pointing to n if the numbers need switching.
b->n = c; // When the switch is finished, b pointing to n would equal to c
}
else
{
c->n = head; // In the else statement, c pointing to n would equal to head if the numbers are finished switching places.
head = c; // After the switching has finshed, c will pass the sorted list to head.
}
}
else
{
c->n = NULL; // In the second else statement, c pointing to n will equal null if there are no switches made.
head = c;// c will pass the current state of the list to head.
}
}
return head;
}
node* reverseList(node* head)
{
node* first = NULL;
node* now = head;
node* n;
while (now != NULL)
{
n = now->n;
now->n = first;
first = now;
now = n;
}
head = first;
return head;
}
void menu()
{
printf("0.menu\n");
printf("1. Insert some nodes into the list\n");
printf("2. Insert node after a previous node\n");
printf("3. Remove a node from the front of the list\n");
printf("4. Remove any node from the list\n");
printf("5. Sort the list\n");
printf("6. Reverse the linked list\n");
printf("-1.quit\n");
}
int main()
{
int instruction = 0;
int x;
node* head = NULL;
node* spare = NULL;
callback show = display;
menu();
while(1)
{
printf("Enter a command(0-6,-1 to quit):");
scanf("%d",&instruction);
if(instruction == -1)
break;
switch(instruction)
{
case 0:
menu();
break;
case 1:
printf("Enter 6 numbers into the list: ");
scanf("%d",&x);
head = beginning(head, x);
traverse(head,show);
break;
case 2:
printf("Enter a number to insert after the previous number:");
scanf("%d",&x);
spare = find(head,x);
if(spare != NULL)
{
printf("Enter the value you wish to insert after:");
scanf("%d",&x, &spare);
head = insertNodeAfter(head, x, spare);
if(head != NULL)
traverse(head,show);
}
else
{
printf("The number %d does not exist",x);
}
break;
case 3:
head = removefirstNode(head);
if(head != NULL)
traverse(head,show);
break;
case 4:
printf("Enter a number that you want to remove:");
scanf("%d",&x);
spare = find(head,x);
if(spare != NULL)
{
removeNode(head,spare);
if(head != NULL)
traverse(head,show);
}
else
{
printf("The number %d does not exist",x);
}
break;
case 5:
head = sortList(head);
if(head != NULL)
traverse(head,show);
break;
case 6:
head = reverseList(head);
if(head != NULL)
traverse(head,show);
break;
}
}
return 0;
}
\ 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