Making basic linked list program help me plz..!ASAP I posted inputs at the botto
ID: 3852735 • Letter: M
Question
Making basic linked list program help me plz..!ASAP
I posted inputs at the bottom.
Directions:
Complete the following prelab assignment using the description given in each section. Be sure to comment your program; otherwise you may lose up to 5 points. You must use proper indent styles; otherwise you may lose up to 5 points. The input file is located on Blackboard under “input4.txt”.
Description:
typedef struct node_ {
int data;
struct node_* next;
}node;
void print_list(node* list);
Parameters:
list: A pointer to the head of a single linked list
Return: none
See example output, prints out the linked list
void free_list(node* list);
Parameters
list: A pointer to a single linked list
Return: None
This function should free each node in the linked list
node* create_list(char* file);
Parameters:
file: The name of the input file
Return: The head of the linked list
This function will open the input file and will build a linked list by malloc()ing a node for each of the numbers and linking them together.
int main(int argc, char* argv[]);
Main will read in an input file as a command line argument and then create a linked list, print it out and then free it.
Sample output:
./a.out input4.txt
1050->2050->2270->3050->3280->3330->3380->4050->4320->4520->4850->4970->4980->NULL
inputs:
1050
2050
2270
3050
3280
3330
3380
4050
4320
4520
4850
4970
4980
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct node {
int data;
struct node *link;
};
struct node *head=NULL, *temp;
void create(){
struct node *curr;
int ele;
printf("Enter:-");
scanf("%d",&ele);
curr=(struct node*)malloc(sizeof(struct node));
curr->data=ele;
curr->link=NULL;
if(head==NULL)
head=curr;
else
temp->link=curr;
temp=curr;
}
void del(){
struct node *t,*pre;
t=head;
int n;
printf("Enter element to delete->");
scanf("%d",&n);
while(t->link!=NULL){
if(t->data==n){
pre->link = t->link;
}
pre=t;
t=t->link;
}
}
void display(){
struct node *t;
t=head;
if(t==NULL)
printf("There is no link list");
else{
while(t!=NULL){
printf("->%d",t->data);
t=t->link;
}
}
}
int main(){
while(1){
int ch;
printf(" Linked List Operation");
printf(" 1.Insert");
printf(" 2.Delete");
printf(" 3.Display");
printf(" 4.Exit");
printf(" Enter your choice");
scanf("%d",&ch);
switch(ch){
case 1:create();break;
case 2:del();break;
case 3:display();break;
case 4:exit(0);
default: printf("Invalid choice ");
}
}
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.