Related
I created the linked list to store employee id and name.
When I tried to print it, it shows only id not an employee name and i also want to exit the program when the user enter -1 and not asking the name its should simply exit the program and display the id and name i am currently using devC++ for compiling my code
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
struct node
{
int id;
char name[20];
struct node *next;
};
struct node *create()
{
struct node *p, *r, *n;
int s, k;
char name[20];
s = sizeof(struct node);
printf("Linked List\n");
printf("Enter id:");
scanf("%d", &k);
printf("Enter name:");
scanf("%s", name);
p = r = NULL;
while(k!=-1)
{
n = (struct node *)malloc(s);
n->id = k;
n->next = NULL;
if(r == NULL)
r = n;
else
p->next=n;
p=n;
printf("Enter the Id or-1 to stop:");
scanf("%d", &k);
printf("Enter the name ");
scanf("%s", name);
}
return(r);
}
void display(struct node *r)
{
printf("\nId Name \n");
while(r != NULL)
{
printf("\n %d", r->id);
printf("\n %s", r->name);
r = r->next;
}
}
int main()
{
struct node *ptr;
ptr = create();
display(ptr);
}
You actually read in the name variable, but you don't move it in your structure.
That said, you could directly read into the structure you allocate, but the tricky part is taking care of not overflowding your buffer when the user input is too big (more than 19 chars).
This could look like this:
#include<stdio.h>
#include<stdlib.h>
struct node {
int id;
char name[20];
struct node *next;
};
struct node *create(void) {
struct node *p, *r;
printf("Linked List\n");
p = r = NULL;
while (1) {
int id;
printf("Enter the Id or-1 to stop:");
scanf("%d", &id);
if (id == -1)
break; // user asked to leave
struct node *n = malloc(sizeof(*n));
// if (n == NULL) exit(-1); as you prefere...
n->id = id;
printf("Enter the name ");
// careful of buffer overflow here
scanf("%19s%*[^\n]", n->name);
n->next = NULL;
if (r == NULL)
r = n;
else
p->next = n;
p = n;
}
return r;
}
void delete(struct node *r) {
while (r != NULL) {
struct node *n = r;
r = r->next;
free(n);
}
}
void display(const struct node *r) {
printf("\nId Name \n");
while (r != NULL) {
printf("%d %s\n", r->id, r->name);
r = r->next;
}
}
int main(int argc, char **argv) {
struct node *ptr = create();
display(ptr);
delete(ptr);
return 0;
}
As a bonus, I also added the free part so that you don't leak.
I am new to C programming. I have created a student database to enter student details into a linked list (in the form of structure "phbook") and save linked list onto text file. The part I cannot get to work is to read the text file records onto the linked list. The program runs but does not update the linked list when I close the program then select to "LOAD FROM EXTERNAL FILE". Please can someone help me figure what the problem is. I know everything worked until I coded the "readFile" and "insertFull" function. Friends have told me that the global variable "struct phbook *list = NULL;is causing the problem. Please let me know.
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct phbook{
int number;
char name[20];
int mark;
struct part *next;
};
struct phbook *list = NULL;
struct phbook *find_student(int number);
void insertFull(struct phbook* list, int number, char Name[10],int mark);
int main(void)
{
int code;
int opt1;
int courses, i, k, j, counter;
for (;;){
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code){
case 1 : insert();
break;
case 2 : search();
break;
break;
case 3 : print();
break;
case 4 :
saveToFile();
break;
case 5 :
readFile();
break;
default: printf("Illegal code\n");
}
printf("\n");
}
}
struct phbook *find_student(int number)
{
struct phbook *p;
for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
if (p != NULL && number == p->number)
return p;
return NULL;
}
void insert(void)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
printf("enter student id");
scanf("%d", &new_node->number);
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
printf("Enter name: ");
scanf("%s", &new_node->name);//readline(new_node->name, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->mark);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
void search(void)
{
int number;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &number);
p = find_student(number);
if (p != NULL){
printf("Name: %s\n", p->name);
printf("Marks: %d\n", p->mark);
}
else
printf("student not found.\n");
}
void print(void)
{
struct phbook *p;
printf("Student_Number Student_Name Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile()
{
FILE* fp;
fp = fopen ("results.txt","w");
struct phbook* curr = list;//he
while(curr!=NULL)
{
fprintf(fp,"%s\n", list->name);
fprintf(fp,"%d\n",curr->number);
fprintf(fp, "%d\n", curr->mark);
curr = curr->next;
}
fclose(fp);
}
void readFile()
{
FILE* fp;
if (!(fp = fopen ("results.txt", "r")))
printf("File NOT Found");
else{
struct phbook *curr;
struct phbook *prev;
char TempName[10];
int TempNumber;
int TempMark;
int done = 0;
int count = 0;
int success;//dummy
curr = list;//sets it to the head (first nde of ll)
if(list == NULL);
printf("List is null\n");
if(curr == NULL)
printf("List is null\n");
while (curr!=NULL)
{
curr = curr->next;
}
while (done == 0)
{
success = fscanf(fp, "%s", TempName);
if (success == 1)
{
success = fscanf(fp, "%d", &TempNumber);
if (success == 1)
{
success = fscanf(fp, "%d", &TempMark);
if(success == 1)
{
insertFull(list,TempNumber,TempName,TempMark);
}
}
}
else
{
done = 1;
}
}
}
}
void insertFull(struct phbook* list, int number, char Name[10],int mark)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL){
printf("db full er1.\n");
return;
}
for (cur = list, prev = NULL; cur!= NULL && new_node->number > cur->number; prev = cur, cur = cur->next);
new_node->number = number;
new_node->mark = mark;
strcpy(new_node->name,Name);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
}
In the procedure
void insertFull(struct phbook* list, int number, char Name[10],int mark)
you are changing your local copy of list, this has no effect on the global phone book list.
You can remove list from the argument lists, thus changing the global variable:
void insertFull(int number, char Name[10],int mark)
this is not strictly good programming, but it is in line with the rest of your code.
I am new to c programming. In my program I have to make a database to store students. The program has to allow to enter marks, id, name and store (as struct)in linked list and also store in file and also read from file into linked list. The only problem I have is that when I close the .exe program and then select to read from .txt file into linked list it does not work. No errors came when I compiled the code but when I AFTERWARDS select DISPLAY REPORT OF ALL STUDENTS no records are shown. This is a minor problem, please suggest how to fix this.
My code is as follows:
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
struct phbook
{
int number;
char name[20];
int mark;
struct part *next;
};
//struct phbook *find_student(int number);
//void insertFull(struct phbook* list, int number, char Name[10],int mark);
struct phbook* insert(struct phbook *list);
struct phbook* insertFull(struct phbook* list,
int number,
char Name[10],
int mark);
void readFile(struct phbook* list);
int main(void)
{
struct phbook *list = NULL;
int code;
int opt1;
int courses, i, k, j, counter;
for (;;)
{
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code)
{
case 1:
list = insert(list);
break;
case 2: //search();
break;
break;
case 3:
print(list);
break;
case 4:
saveToFile(list);
break;
case 5:
readFile(list);
break;
default:
printf("Illegal code\n");
}
printf("\n");
}
}
/*struct phbook *find_student(int number)
{
struct phbook *p;
for (p = list; p != NULL && number != p->number; p = p->next);//was sorted
if (p != NULL && number == p->number)
return p;
return NULL;
}*/
struct phbook* insert(struct phbook *list)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL)
{
printf("db full er1.\n");
return NULL;
}
printf("enter student id");
scanf("%d", &new_node->number);
for (cur = list, prev = NULL; cur != NULL && new_node->number > cur->number;
prev = cur, cur = cur->next)
;
printf("Enter name: ");
scanf("%s", &new_node->name); //readline(new_node->name, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->mark);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
return list;
}
void search(void)
{
int number;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &number);
p = find_student(number);
if (p != NULL)
{
printf("Name: %s\n", p->name);
printf("Marks: %d\n", p->mark);
}
else
printf("student not found.\n");
}
void print(struct phbook *list)
{
struct phbook *p;
printf("Student_Number Student_Name Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->number, p->name, p->mark);
}
void saveToFile(struct phbook *list)
{
FILE* fp;
fp = fopen("results.txt", "w");
struct phbook* cur = list; //he
while (cur != NULL)
{
fprintf(fp, "%s\n", list->name);
fprintf(fp, "%d\n", cur->number);
fprintf(fp, "%d\n", cur->mark);
cur = cur->next;
}
fclose(fp);
}
void readFile(struct phbook* list)
{
FILE* fp;
if (!(fp = fopen("results.txt", "r")))
printf("File NOT Found");
else
{
struct phbook *cur;
struct phbook *prev;
char TempName[10];
int TempNumber;
int TempMark;
int done = 0;
int count = 0;
int success; //dummy
cur = list; //sets it to the head (first nde of ll)
if (list == NULL)
;
printf("List is null\n");
if (cur == NULL)
printf("List is null\n");
while (cur != NULL)
{
cur = cur->next;
}
while (done == 0)
{
success = fscanf(fp, "%s", TempName);
if (success == 1)
{
success = fscanf(fp, "%d", &TempNumber);
if (success == 1)
{
success = fscanf(fp, "%d", &TempMark);
if (success == 1)
{
insertFull(list, TempNumber, TempName, TempMark);
}
}
}
else
{
done = 1;
}
}
}
}
struct phbook* insertFull(struct phbook* list,
int number,
char Name[10],
int mark)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
new_node = (struct phbook*) malloc(sizeof(struct phbook));
if (new_node == NULL)
{
printf("db full er1.\n");
return;
}
for (cur = list, prev = NULL; cur != NULL && new_node->number > cur->number;
prev = cur, cur = cur->next)
;
new_node->number = number;
new_node->mark = mark;
strcpy(new_node->name, Name);
new_node->next = cur;
if (prev == NULL)
list = new_node;
else
prev->next = new_node;
return list;
}
the screenshot is this:
Screen shot was already MIA when I got here - user4581301
If you execute your application the main list will be null.
When you then call readFile(), you will not update the list in main. You just pass the pointer by value, so that you'll update the local paramater of that function. By the way, the call to insertFull() has the almost the same problem: it returns the list following the insertion, but you ignore this return.
Change these two functions, so that they both return the list, like you do for insert():
struct phbook* readFile(struct phbook* list)
{
...
list = insertFull(list, TempNumber, TempName, TempMark);
...
return list;
}
and of course adapt your function prototype at the beginning, and updated the menu handling:
case 5:
list = readFile(list);
break;
the following code compiles cleanly
However, it will not link because the function: find_student() is commented out.
Examination of the code indicates it will not fully perform all the desired functionality. So you will still have to debug the execution.
Suggest using a debugger, like gdb
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#define MAX_NAME_LEN (20)
struct phbook
{
int studentID;
char studentName[ MAX_NAME_LEN ];
int studentGrade;
struct phbook *next;
};
// prototypes
struct phbook* insert(struct phbook* );
struct phbook* find_student( int );
void insertFull( struct phbook*, int, char *, int );
void readFile ( struct phbook* );
void print ( struct phbook* );
void saveToFile( struct phbook* );
int main(void)
{
struct phbook *list = NULL;
int code;
for (;;)
{
printf("Enter operation code: \n");
printf("(1) ADD NEW STUDENT DETAILS: \n");
printf("(2) SEARCH STUDENT DETAILS: \n");
printf("(3) DISPLAY REPORT OF ALL STUDENTS: \n");
printf("(4) SAVE ALL STUDENT RECORDS TO EXTERNAL FILE: \n");
printf("(5) LOAD ALL STUDENT RECORDS FROM EXTERNAL FILE: \n");
scanf(" %d", &code);
switch (code)
{
case 1:
list = insert(list);
break;
case 2: //search();
break;
break;
case 3:
print(list);
break;
case 4:
saveToFile(list);
break;
case 5:
readFile(list);
break;
default:
printf("Illegal code\n");
} // end switch
printf("\n");
} // end for()
} // end function: main
#if 0
struct phbook *find_student(int studentID)
{
struct phbook *p;
for (p = list; p != NULL && studentID != p->studentID; p = p->next);//was sorted
return p;
} // end function: find_student
#endif
struct phbook* insert(struct phbook *list)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
if( NULL == (new_node = malloc(sizeof(struct phbook)) ) )
{
perror( "malloc for struct phbook failed");
return NULL;
}
// implied else malloc successful
printf("enter student id");
scanf("%d", &new_node->studentID);
for (cur = list, prev = NULL;
cur != NULL && new_node->studentID > cur->studentID;
prev = cur, cur = cur->next)
{
;
}
printf("Enter studentName: ");
scanf("%19s", new_node->studentName); //readline(new_node->studentName, NAME_LEN)
printf("Enter MARK: ");
scanf("%d", &new_node->studentGrade);
new_node->next = cur;
if (!prev)
{
list = new_node;
}
else
{
prev->next = new_node;
}
return list;
} // end function: search
void search(void)
{
int studentID;
struct phbook *p;
printf("Enter ID: ");
scanf("%d", &studentID);
if( NULL == (p = find_student(studentID) ) )
{
printf("studentName: %s\n", p->studentName);
printf("Marks: %d\n", p->studentGrade);
}
else
{
printf("student not found.\n");
}
} // end function: search
void print(struct phbook *list)
{
struct phbook *p;
printf("Student_Number Student_studentName Student_Mark\n");
for (p = list; p != NULL; p = p->next)
printf("%7d %-25s %d\n", p->studentID, p->studentName, p->studentGrade);
} // end function: print
void saveToFile(struct phbook *list)
{
FILE* fp;
fp = fopen("results.txt", "w");
struct phbook* cur = list; //he
while (cur != NULL)
{
fprintf(fp, "%s\n", list->studentName);
fprintf(fp, "%d\n", cur->studentID);
fprintf(fp, "%d\n", cur->studentGrade);
cur = cur->next;
}
fclose(fp);
} // end function: saveToFile
void readFile( struct phbook* list)
{
FILE* fp = NULL;
if (!(fp = fopen("results.txt", "r")))
{
perror( "fopen for read of results.txt failed");
return;
}
//implied else, fopen successful
char TempstudentName[10];
int TempNumber;
int TempMark;
while (3 == fscanf(fp, " %19s %d %d", TempstudentName, &TempNumber, &TempMark ) )
{
insertFull(list, TempNumber, TempstudentName, TempMark);
}
} // end function; readFile
void insertFull(struct phbook* list,
int studentID,
char studentName[10],
int studentGrade)
{
struct phbook *cur;
struct phbook *prev;
struct phbook *new_node;
if( NULL == (new_node = malloc(sizeof(struct phbook)) ) )
{
perror( "malloc for size of struct phbook failed" );
return;
}
for (cur = list, prev = NULL;
cur != NULL && new_node->studentID > cur->studentID;
prev = cur, cur = cur->next)
{
;
}
new_node->studentID = studentID;
new_node->studentGrade = studentGrade;
strcpy(new_node->studentName, studentName);
new_node->next = cur;
if (!prev)
{
list = new_node;
}
else
{
prev->next = new_node;
}
} // end function: insertFull
I did not add all the necessary error checking, so you will have to do that.
I added the missing prototypes
I corrected several minor coding errors
My text file reads like this:
George Washington, 2345678
John Adams, 3456789
Thomas Jefferson, 4567890
James Madison, 0987654
James Monroe, 9876543
John Quincy Adams, 8765432
Andrew Jackson, 7654321
Martin Van Buren, 6543210
Can someone offer insight on how I get my insert function to add the name and ID number to the end of the linked list? When I run the code an select option 1 it skips over the add name and only asks to enter the integer. After that nothing happens.
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
//Creates node for holding student's information
struct node
{
char name [50];
int id;
struct node *next;
}*head;
//Create Function Prototypes
void readDataFile ();
void insert(char *inName, char *inID);
void display(struct node *d);
int deleteID(int num);
void deleteName(char dname);
//Main function
int main()
{
//Declare variables
int i, num, id;
char *name;
char nameDelete [50];
char nameInsert [50];
struct node *n;
//initialize link list
head = NULL;
//Read in file
readDataFile();
//Create list of operations utilized in program
while (1)
{
printf("\nList Operations\n");
printf("===============\n");
printf("1.Insert\n");
printf("2.Display\n");
printf("3.Delete by ID\n");
printf("4.Delete by Name\n");
printf("5.Exit\n");
printf("Enter your choice : ");
if(scanf("%d", &i) <= 0)
{
printf("Enter only an Integer\n");
exit(0);
}
else
{
switch(i)
{
case 1:
printf("Enter the name to insert:\n");
scanf("%[^\n]s", nameInsert);
getchar();
printf("Enter the ID associated with the name: ");
scanf("%d", &id);
insert(nameInsert, id);
break;
case 2:
if (head == NULL)
printf("List is Empty\n");
else
{
printf("Elements in the list are:\n");
}
display(n);
break;
case 3:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter the ID number to delete: ");
scanf("%d", &id);
}
if(deleteID(id))
printf("%d deleted successfully \n", id);
else
printf("%d not found in the list\n", id);
break;
case 4:
if(head == NULL)
printf("List is Empty\n");
else
{
printf("Enter name to delete: ");
gets(nameDelete);
}
case 5:
return 0;
default:
printf("Invalid option\n");
}
}
}
return 0;
}
//Define the functions
//Function to read in the data file
void readDataFile()
{
//Initialize the link the list
struct node *temp;
temp = malloc(sizeof(struct node));
temp->next = NULL;
head = temp;
//Open up the file
FILE *fp;
fp = fopen("AssignmentOneInput.txt", "r");
//Use memset function to copy the characters in string
char string[100];
memset(string, 0, 100);
//Create while loop scan in the contents of the file
while(fgets(string, 100, fp) != NULL)
{
sscanf(string, "%20[^,], %d", temp->name, &temp->id);
temp->next = malloc(sizeof(struct node));
temp = temp->next;
temp->next = NULL;
}
fclose(fp);
}
//Function to insert a name and ID number
void insert(char *inName, char *inID)
{
//Create temporary and helper nodes
struct node *temp, *helper;
//Allocate the memory for the temporary node
temp = (struct node *)malloc(sizeof(struct node));
//Convert character into integer value
int intID = atoi(inID);
//Set the data in the node
strcpy(temp->name, inName);
temp->id = intID;
temp->next = NULL;
//Create new node and add to it
helper = (struct node *)head;
while(helper->next != NULL)
{
helper = helper->next;
}
helper->next = temp;
helper = temp;
helper->next = NULL;
}
Once you enter 1 in selection option the standard input has both 1 and newline character, which you are expecting immediately in the next scanf statement.
change the scanf("%[^\n]s", nameInsert); into scanf("%s", nameInsert);
or use getchar() immediately after getting selection option.
When i compile this link list my Console Jams after entering the first two intergers. The purpose of the programme is to save the input from scanf into Memory and then output them onto the screen, after this i intend to have the programme save the inputs into a text file.
#include <stdio.h>
#include <stdlib.h>
/*********************************************************
* Node to represent a packet which includes a link reference*
* a link list of nodes with a pointer to a packet Struct *
**********************************************************/
struct Packet {
unsigned int Source;
unsigned int Destination;
unsigned int Type;
unsigned int Port;
char *Data;
struct Packet *next;
};
typedef struct Packet node; // Removes the need to constantly refer to struct
/*********************************************************
* Stubs to fully declared functions below *
**********************************************************/
void Outpacket(node **head);
void push(node **head, node **aPacket);
node* pop(node **head);
int main() {
/*********************************************************
* pointers for the link list and the temporary packeyt to *
* insert into the list *
**********************************************************/
node *pPacket, *pHead = NULL;
/*********************************************************
* Create a packet and also check the HEAP had room for it *
**********************************************************/
pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source Number:\n");
scanf("%i", pPacket->Source);
printf("Enter Destination Number:\n");
scanf("%i", pPacket->Destination);
printf("Enter Type Number:\n");
scanf("%i", pPacket->Type);
printf("Enter Port Number:\n");
scanf("%i", pPacket->Port);
printf("Enter Data Number:\n");
scanf("%c", pPacket->Data);
pPacket->next = NULL;
/*********************************************************
* Push the Packet onto the selected Link List, the function *
* is written so the program will support multiple link *
* list if additional 'pHead' pointers are created. *
* *
**********************************************************
* NOTE: The push parameters are using references to the *
* pointers to get round the pass by value problem caused *
* by the way C handles parameters that need to be *
* modified *
**********************************************************/
push(&pHead, &pPacket);
pPacket = (node *)malloc(sizeof(node));
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source Number:\n");
scanf("%i", pPacket->Source);
printf("Enter Destination Number:\n");
scanf("%i", pPacket->Destination);
printf("Enter Type Number:\n");
scanf("%i", pPacket->Type);
printf("Enter Port Number:\n");
scanf("%i", pPacket->Port);
printf("Enter Data Number:\n");
scanf("%c", pPacket->Data);
pPacket->next = NULL;
push(&pHead, &pPacket);
/*********************************************************
* Display the Link List 'pHead' is passed as a reference *
**********************************************************/
Outpacket(&pHead);
if(pPacket = pop(&pHead))
{
printf("pPacket %s\n", pPacket->Data);
free(pPacket);
};
Outpacket(&pPacket);
while(pPacket = pop(&pHead)) {
free(pPacket);
}
return 0;
}
void Outpacket(node **head)
{
/*********************************************************
* Copy Node pointer so as not to overwrite the pHead *
* pointer *
**********************************************************/
node *pos = *head;
printf("Packet list\n");
/*********************************************************
* Walk the list by following the next pointer *
**********************************************************/
while(pos != NULL) {
printf("Source: %i Destination: %i Type: %i Data: %i \n", pos->Source, pos->Destination, pos->Type, pos->Data, pos->next);
pos = pos->next ;
}
printf("End of Packet\n\n");
}
void push(node **head, node **aPacket)
{
/*********************************************************
* Add the cat to the head of the list (*aCat) allows the *
* dereferencing of the pointer to a pointer *
**********************************************************/
(*aPacket)->next = *head;
*head = *aPacket;
}
node *pop(node **head)
{
/*********************************************************
* Walk the link list to the last item keeping track of *
* the previous. when you get to the end move the end *
* and spit out the last Packet in the list *
**********************************************************/
node *curr = *head;
node *pos = NULL;
if (curr == NULL)
{
return NULL;
} else {
while (curr->next != NULL)
{
pos = curr;
curr = curr->next;
}
if (pos != NULL) // If there are more packets move the reference
{
pos->next = NULL;
} else { // No Packets left then set the header to NULL (Empty list)
*head = NULL;
}
}
return curr;
}
Thanks
#include <stdio.h>
#include <stdlib.h>
#include <conio.h>
struct Packet {
unsigned int Source;
unsigned int Destination;
unsigned int Type;
unsigned int Port;
char *Data;
struct Packet *next;
};
typedef struct Packet* node;
node getnode(void){
node p;
p=malloc(sizeof(struct Packet));/*change1*/
return p;
}
void Outpacket(node *head);
void push(node *head, node *aPacket);/*change 2*/
node pop(node *head);
int main() {
node pPacket = NULL;
node pHead = NULL;
pPacket = getnode();/*change 3*/
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source :\n");
scanf("%d", &(pPacket->Source));
printf("Enter Destination Number:\n");
scanf("%d", &(pPacket->Destination));
printf("Enter Type Number:\n");
scanf("%d", &(pPacket->Type));
printf("Enter Port Number:\n");
scanf("%d", &(pPacket->Port));
printf("Enter Data Number:\n");
pPacket->Data=malloc(100);/*change 4,might overflor,so you decide how much memory you want*/
scanf("%s",pPacket->Data);
pPacket->next = NULL;
push(&pHead, &pPacket);
pPacket = getnode();/*change 5*/
if (pPacket == NULL)
{
printf("Error: Out of Memory\n");
exit(1);
}
printf("Enter Source Number:\n");
scanf("%d", &(pPacket->Source));
printf("Enter Destination Number:\n");
scanf("%d", &(pPacket->Destination));
printf("Enter Type Number:\n");
scanf("%d", &(pPacket->Type));
printf("Enter Port Number:\n");
scanf("%d", &(pPacket->Port));
printf("Enter Data Number:\n");
pPacket->Data=malloc(100);/*change 6*/
scanf("%s",pPacket->Data);
pPacket->next = NULL;
push(&pHead, &pPacket);
Outpacket(&pHead);
if(pPacket == pop(&pHead))/*change 7,this was a classic error*/
{
printf("pPacket %s ", pPacket->Data);
free(pPacket);
}
Outpacket(&pPacket);
while(pPacket == pop(&pHead))/*change 8,again the same error,please take care of this*/{
free(pPacket);
}
getch();
return 0;
}
void Outpacket(node *head)
{
node pos = *head;
printf("Packet list\n");
while(pos != NULL) {
printf("Source: %d Destination: %d Type: %d Data: %s \n", pos->Source, pos->Destination, pos->Type,pos->Data);/*change 9,what you wrote was absurd, I think*/
pos = pos->next ;
}
printf("End of Packet\n\n");
}
void push(node *head, node *aPacket)
{
(*aPacket)->next = *head;
*head = *aPacket;
}
node pop(node *head)
{
node curr = *head;
node pos = NULL;
if (curr == NULL)
{
return NULL;
} else {
while (curr->next != NULL)
{
pos = curr;
curr = curr->next;
}
if (pos != NULL)
{
pos->next = NULL;
} else {
*head = NULL;
}
}
return curr;
}
This code runs on my machine perfectly.Please Review the changes and compare both the codes,ask me if you don't understand any change.