In C Programming: // This function creates an empty list pointer and returns it.
ID: 3889076 • Letter: I
Question
In C Programming:
// This function creates an empty list pointer and returns it.
List* createList ( );
//This function adds an integer item to the end of the list by taking a list pointer and an integer item to be added and returns the updated list pointer.
List* addItemAtEnd (List*, int);
//This function adds an integer item to the beginning of the list by taking a list pointer and an integer item to be added and returns the updated list pointer.
List* addItemInBeginning (List*, int);
//This function prints out the items in the list passed through the parameter.
void printList (List);
Structure given in header file:
#include <stdio.h>
#include <stdlib.h>
typedef struct nodestruct {
int item;
struct nodestruct *next;
} Node;
typedef struct {
int size; // Number of items on user’s list
Node *head, *tail;
} List;
List* createList ();
List* addItemAtEnd (List*, int);
List* addItemInBeginning (List*, int);
void printList (List);
Node* createNode(int);
In main:
First, create a List ADT based on the structure given in the header file. Then, 'generate' 13 random integer numbers between 1 and 13. Print out the 'generated' numbers on the console separated by space. Next, add first 6 of the 13 numbers one by one to the beginning of the list and the remaining 7 numbers one by one to the end of the list. Print out the list on the console. Finally, terminate the program.
Example output:
mwc-070138:~ $ gcc main.c lab5.c -Wall -Werror
mwc-070138:~ $ ./a.out
Randomly 'generated' numbers: 1 1 2 4 3 5 13 10 3 11 2 6 5
Items on the linked list: 5 3 4 2 1 1 13 10 3 11 2 6 5
Have a nice day!!!!!
mwc-070138:~ $
Explanation / Answer
#include<stdio.h>
#include<stdlib.h>
struct node
{
int key;
struct node *next;
struct node *prev;
};
struct node* tail = NULL;
struct node* head = NULL;
void Display_Front(struct node *n)
{
while (n != NULL)
{
printf(" %d ", n->key);
n = n->next;
}
}
void Display_Back(struct node *n)
{
while (n != NULL)
{
printf(" %d ", n->key);
n = n->prev;
}
}
int Length(struct node *node)
{
int count = 0;
while (node != NULL)
{
count++;
node = node->next;
}
return count;
}
void push(struct node** head, int key)
{
struct node* curr = (struct node*) malloc(sizeof(struct node));
curr->key = key;
curr->next = *head;
*head = curr;
}
struct node * BuildOne(int key)
{
struct node* head = NULL;
head = (struct node*)malloc(sizeof(struct node));
head->key = key;
head->next = NULL;
return head;
}
struct node * getTail(struct node *n)
{
struct node * curr = n;
while (curr->next != NULL)
{
curr = curr->next;
}
return curr;
}
void Push_Tail(int data){
struct node* curr = (struct node*) malloc(sizeof(struct node));
curr->key = data;
curr->next = NULL;
curr->prev = tail;
tail->next = curr;
tail = curr;
}
void Pop_Head(){
if(head==NULL)
return;
struct node *curr = head;
head = head->next;
free(curr);
}
void Pop_Tail(){
if(head ==NULL || tail==NULL)
return;
struct node *curr = head;
struct node *prev = NULL;
while (curr->next != NULL)
{
prev = curr;
curr = curr->next;
}
if(curr!=NULL && prev!=NULL){
prev->next = NULL;
tail = prev;
}
if(prev==NULL){
tail = NULL;
}
}
int main()
{
int i=0;
printf(" Randomly generated numbers: ");
for(i = 0 ; i<13;++i){
int r = rand()%13 + 1;
printf("%d ",r);
if(i==0){
head = BuildOne(r);
tail = getTail(head);
}
else if(i<=5)
push(&head, r);
else
Push_Tail(r);
}
printf(" Orignal List: ");
Display_Front(head);
printf(" Length is: %d",Length(head));
return 0;
}