Skip to content

Instantly share code, notes, and snippets.

@arjolpanci
Created May 22, 2019 17:14
Show Gist options
  • Save arjolpanci/cd7c67f64dbf5b05ab569dd79a62d3f4 to your computer and use it in GitHub Desktop.
Save arjolpanci/cd7c67f64dbf5b05ab569dd79a62d3f4 to your computer and use it in GitHub Desktop.
Projekti
#include <stdio.h>
#include <stdlib.h>
struct Number
{
int num;
struct Number * next;
};
typedef struct Number node;
typedef struct Number *ndPtr;
void builtRandList(ndPtr *First,int n);
void addNodeEnd(ndPtr *First, node n);
void addNodeFront(ndPtr *First, node n);
void printList(ndPtr First);
int getSize(ndPtr First);
void deleteEndNode(ndPtr *First);
void deleteFrontNode(ndPtr *First);
void deleteNodeVal(ndPtr *First,node nd);
void deleteNodeAtPos(ndPtr *First,int pos);
void deleteAll(ndPtr *First);
void insertSorted(ndPtr *First, node n);
void displyMenu()
{
system("cls");
printf("*********************************\n");
printf("* EPOKA UNIVERSITY *\n");
printf("* CEN 110 - C Programming! *\n");
printf("* ==Singly Linked List== *\n");
printf("* *\n");
printf("* List Of Operations *\n");
printf("* --------------------------- *\n");
printf("* 1. Built random list. *\n");
printf("* 2. Insert to the END *\n");
printf("* 3. Insert to the FRONT *\n");
printf("* 4. Insert Sorted *\n");
printf("* 5. deleteEndNode *\n");
printf("* 6. deleteFrontNode *\n");
printf("* 7. deleteNodeVal *\n");
printf("* 8. deleteNodeAtPos *\n");
printf("* 9. deleteAllNodes *\n");
printf("* 10. DisplayList *\n");
printf("* 11. Size *\n");
printf("* 12. Exit *\n");
printf("*********************************\n");
}
int main()
{
ndPtr First, tmp;
First=NULL;
node nd;
int i;
int n;
while(1)
{
displyMenu();
printList(First);
printf("\nEnter your choice: ");
scanf("%d",&i);
switch(i)
{
case 1:
printf("Enter the number of nodes: ");
scanf("%d",&n);
builtRandList(&First,n);
break;
case 2:
printf("Enter the number: ");
scanf("%d",&nd.num);
addNodeEnd(&First,nd);
break;
case 3:
printf("Enter the number: ");
scanf("%d",&nd.num);
addNodeFront(&First,nd);
break;
case 4:
printf("Enter the number: ");
scanf("%d",&nd.num);
insertSorted(&First,nd);
break;
case 5:
deleteEndNode(&First);
break;
case 6:
deleteFrontNode(&First);
printList(First);
break;
case 7:
printf("Enter the number to be deleted : ");
scanf("%d",&nd.num);
deleteNodeVal(&First,nd);
break;
case 8:
printf("Enter the position of the number to be deleted (0 - n-1): ");
int pos;
scanf("%d",&pos);
deleteNodeAtPos(&First,pos);
break;
case 9:
deleteAll(&First);
break;
case 10:
printList(First);
system("pause");
break;
case 11:
printf("Size of the list is: %d\n",getSize(First));
system("pause");
break;
case 12:
return 0;
default:
printf(" Invalid option!\n");
system("pause");
}
}
return 0;
}
void builtRandList(ndPtr *First,int n)
{
int i;
for(i=0; i<n; i++)
{
ndPtr newNode;
newNode=(ndPtr)malloc(sizeof(node));
newNode->num=rand()%101;
newNode->next=NULL;
if((*First)==NULL)
{
(*First)=newNode;
}
else
{
ndPtr Last=*First;
while(Last->next!=NULL)
Last=Last->next;
Last->next=newNode;
Last=newNode;
}
}
}
void addNodeEnd(ndPtr *First, node n)
{
ndPtr temp;
ndPtr newNode = (ndPtr)malloc(sizeof(node));
newNode->num=n.num;
newNode->next=NULL;
temp = *First;
while(temp->next!=NULL){
temp=temp->next;
}
temp->next = newNode;
temp=newNode;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment