Utilizes SIX of the following items to create a CREATIVE C program: • Strings •
ID: 3718163 • Letter: U
Question
Utilizes SIX of the following items to create a CREATIVE C program:
• Strings
• 2D Arrays
• File I/O
• Command Line Arguments
• Pass by Reference
• Structures
• Linked Lists (Pointers)
• Sorting
• Recursion
• Stack/Queue
• Custom Header Files
The program should NOT be a simple implementation of six of the topics listed above, but should make sense as a semi-real world example! This program should be creative and not just simple functions. Please include comments and a list of the items used for the program!
Explanation / Answer
Answer:-
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
//declaration of structure
struct node
{
char name[10]; //declaration of name by string type
struct node *next; // declaration of pointers
};
struct node *create(struct node *);
struct node *sort(struct node *);
void display(struct node *);
void main()
{
struct node *start=NULL;
start=create(start);
display(start);
start=sort(start);
display(start);
getch();
}
struct node *create(struct node *start)
{
struct node *p,*q;
char x[10];
while (1)
{
printf(" Enter the data to linked list or press # to exit:");
scanf(" %s",x);
if(strcmp(x,"#")==0)
break;
if(start==NULL)
{
p=(struct node *)malloc(sizeof(struct node));
p->next=NULL;
strcpy(p->name,x);
start=p;
}
else
{
q=start;
while(q->next!=NULL)
q=q->next;
p=(struct node *)malloc(sizeof(struct node));
p->next=NULL;
strcpy(p->name,x);
q->next=p;
}
}
return start;
}
void display(struct node *start) //passing value by reference
{
struct node *t;
printf(" Linked List ");
while(start->next!=NULL)
{
printf("--> %s",start->name);
start=start->next;
}
printf("--> %s",start->name);
}
struct node *sort(struct node *start) //sorting linked list
{
struct node *p,*q;
char temp[10];
p=start;
while(p->next!=NULL)
{ q=p;
p=p->next;
if(strcmp(q->name,p->name)>=0)
{
strcpy(temp,q->name);
strcpy(q->name,p->name);
strcpy(p->name,temp);
//printf("Sorted data:");
}
}
return start;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.