Actively programming today 5/17/2016. ( Not happy PPLUG (Pikes Peak Linux Users Group – is not active, and I’m a SATLUG member from San Antonio, Texas. I was hoping to find an active Linux group here in Colorado Springs.
PROGRAMMING AND COMPLETING A LINKED LIST .
/// My C programming, completed linked list with add, remove, search, and display functions.
#include
<stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
struct node
{
int data;
int nodenum;
char empname[15];
struct node* next;
};
int CreateList(struct node* head);
int DisplayList(struct node* traverse);
int DeleteNode(struct node* nodekiller);
int AddNode(struct node* addnode);
int Search(struct node* seekit);
int main()
{
struct node* nodeptr = (struct node*)malloc(sizeof(struct node));
int menu;
restart:
printf(“\tTo create a list enter the number 1\n”);
printf(“\tTo display a previously created list enter the number 2\n”);
printf(“\tTo delete an entry enter 4\n”);
printf(“\tTo Add an entry into a list enter 5\n”);
printf(“\tTo Search for an employee name enter 6\n”);
printf(“\tTo exit program enter 3\n”);
printf(“Enter choice: “);
scanf(“%d”,&menu);
switch(menu)
{
case 1:
nodeptr = CreateList(nodeptr);
break;
case 2:
DisplayList(nodeptr);
break;
case 3:
return 0;
break;
case 4:
DeleteNode(nodeptr);
break;
case 5:
AddNode(nodeptr);
break;
case 6:
Search(nodeptr);
break;
default:
printf(“\nYou did not enter the correct choice try again\n”);
system(“pause”);
goto restart;
}
goto restart;
return 0;
}
int CreateList(struct node* head)
{
int choice;
int i;
struct node* firstNode = (struct node*)malloc(sizeof(struct node));
struct node* seeker = (struct node*)malloc(sizeof(struct node));
printf(“\t Please enter a number of employee’s to input into a list\n”);
printf(“\t Enter number here: “);
scanf(“%d”,&choice);
seeker = firstNode;
for(i = 1; i <= choice; i++)
{
seeker->nodenum = i;
printf(“\n Enter employee #%d, name: “,i);
scanf(“%s”,&seeker->empname);
printf(“\n Enter Badge #; “);
scanf(“%d”,&seeker->data);
seeker->next = (struct node*)malloc((sizeof(struct node)));
seeker = seeker->next;
}
seeker->data = 99999;
system(“cls”);
return firstNode;
}
int DisplayList(struct node* traverse)
{
system(“cls”);
while(traverse->data != 99999)
{
if(traverse->data == 0)
{
printf(“\nEmpty List please use option one to create a list\n”);
break;
}
printf(“Employee Name: %s Badge#: %d \n”,traverse->empname,traverse->data);
traverse = traverse->next;
}
return 0;
}
int DeleteNode(struct node* nodekiller)
{
bool Done = false;
printf(“\tPlease enter the name of a terminated\n\temployee to remove from the list\n”);
printf(“Enter Name: “);
char name[15];
scanf(“%s”,&name);
while(Done == false)
{
if(nodekiller->next->data == 99999)
{
nodekiller->data = 0;
strcpy(nodekiller->empname,”Error”);
break;
}
if(strcmp(nodekiller->next->empname,name) == 0)
{
struct node* temp = (struct node*)malloc(sizeof(struct node));
temp = nodekiller->next;
nodekiller->next = nodekiller->next->next;
free(temp);
temp = NULL;
Done = true;
}
nodekiller = nodekiller->next;
}
return 0;
}
int AddNode(struct node* addnode)
{
bool Done = false;
while(Done == false)
{
if(addnode->next->data == 99999)
{
struct node* tempnode = (struct node*)malloc(sizeof(struct node));
tempnode->next = addnode->next;
addnode->next = tempnode;
printf(“Enter Employee’s Name:”);
scanf(“%s”,&tempnode->empname);
printf(“\nEnter Badge#:”);
scanf(“%s”,&tempnode->data);
Done = true;
break;
}
addnode = addnode->next;
}
}
int Search(struct node* seekit)
{
printf(“Please enter an Employee name to Search for\n”);
printf(“Enter it here: “);
char findname[15];
scanf(“%s”,&findname);
while(seekit->data != 99999)
{
if(strcmp(seekit->empname,findname) == 0)
printf(“\nFOUND: Employee: %s, Badge#: %d\n\n”,seekit->empname,seekit->data);
seekit = seekit->next;
}
return 0;
}