Program 7


Program 7



#include<stdio.h>

#include<string.h>

#include<stdlib.h>

struct node

{

char usn[25], name[25], branch[25];

int sem;

long int phoneNo;

struct node *link;

};

typedef struct node * NODE;

NODE first = NULL;

int count = 0;

NODE createStudentNode()

{

char us[20], nam[20], bran[20];

int se;

long int pn;

printf("\nEnter the usn,Name,Branch, sem,PhoneNo of the student: \n");

scanf("%s %s %s %d %ld", us, nam, bran, &se, &pn);

NODE studentNode;

studentNode = (NODE)malloc(sizeof(struct node));

if(studentNode == NULL)

{

printf("\nMemory is not available");

exit(0);

}

studentNode->link = NULL;

strcpy(studentNode->usn, us);

strcpy(studentNode->name, nam);

strcpy(studentNode->branch, bran);

studentNode->sem = se;

studentNode->phoneNo = pn;

count++;

return studentNode;

}

NODE insertAtFront()

{

NODE temp;

temp = createStudentNode();

if(first == NULL)

{

return temp;

}

temp->link = first;

return temp;

}

NODE deleteAtFront()

{

NODE temp;

if(first == NULL)

{

printf("\nLinked list is empty");

return NULL;

}

if(first->link == NULL)

{

printf("\nThe Student node with usn:%s is deleted ", first->usn);

count--;

free(first);

return NULL;

}

temp = first;

first = first->link;

printf("\nThe Student node with usn:%s is deleted",temp->usn);

count--;

free(temp);

return first;

}

NODE insertAtEnd()

{

NODE cur, temp;

temp = createStudentNode();

if(first == NULL)

{

return temp;

}

if(first->link == NULL)

{

first->link = temp;

return first;

}

cur = first;

while(cur->link !=NULL)

{

cur = cur->link;

}

cur->link = temp;

return first;

}

NODE deleteAtEnd()

{

NODE cur, prev;

if(first == NULL)

{

printf("\nLinked List is empty");

return NULL;

}

if(first->link == NULL)

{

printf("\nThe student node with the usn:%s is deleted", first->usn);

free(first);

count--;

return NULL;

}

prev = NULL;

cur = first;

while(cur->link != NULL)

{

prev = cur;

cur = cur->link;

}

printf("\nThe student node with the usn:%s is deleted",cur->usn);

free(cur);

prev->link = NULL;

count--;

return first;

}

void displayStatus()

{

NODE cur;

int nodeNo = 1;

cur = first;

printf("\nThe contents of SLL: \n");

if(cur == NULL)

printf("\nNo Contents to display in SLL \n");

while(cur!=NULL)

{

printf("\n||%d||", nodeNo);

printf(" USN:%s|", cur->usn);

printf(" Name:%s|", cur->name);

printf(" Branch:%s|", cur->branch);

printf(" Sem:%d|", cur->sem);

printf(" Ph:%ld|", cur->phoneNo);

cur = cur->link;

nodeNo++;

}

printf("\n No of student nodes is %d \n",count);

}

void stackDemoUsingSLL()

{

int ch;

while(1)

{

printf("\n~~~Stack Demo using SLL~~~\n");

printf("\n1:Push operation \n2: Pop operation \n3: Display \n4:Exit \n");

printf("\nEnter your choice for stack demo");

scanf("%d", &ch);

switch(ch)

{

case 1: first = insertAtFront();

break;

case 2: first = deleteAtFront();

break;

case 3: displayStatus(); break;

default: return;

}

}

}

void main()

{

int ch, i, n;

while(1)

{

printf("\n~~~Menu~~~");

printf("\nEnter your choice for SLL operation \n");

printf("\n1:Create SLL of Student Nodes");

printf("\n2:DisplayStatus");

printf("\n3:InsertAtEnd");

printf("\n4:DeleteAtEnd");

printf("\n5:Stack Demo using SLL(Insertion and Deletion at Front)");

printf("\n6:Exit \n");

printf("\nEnter your choice:");

scanf("%d", &ch);

switch(ch)

{

case 1 : printf("\nEnter the no of students: ");

scanf("%d", &n);

for(i=1; i<=n; i++)

first = insertAtFront();

break;

case 2: displayStatus();

break;

case 3: first = insertAtEnd();

break;

case 4: first = deleteAtEnd();

break;

case 5: stackDemoUsingSLL();

break;

case 6: exit(0);

default: printf("\nPlease enter the valid choice");

}

}

}

No comments:

Post a Comment