Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <stdlib.h>
- #include <assert.h>
- struct Node {
- int data;
- struct Node *next;
- };
- typedef struct Node *Node;
- Node Create();
- void List(Node *A);
- void Append(Node *A, int d);
- void Remove(Node *A);
- int GetHead(Node A);
- int GetTail(Node A);
- int isEmpty(Node A);
- void main() {
- int i, n, c;
- Node *head = NULL;
- printf("How many elements to enter?: ");
- scanf("%d", &n);
- c=n;
- printf("Enter the value: ");
- for (i = 0; i < c; i++) {
- if (i==0)
- head = Create();
- else
- {
- scanf("%d", &n);
- Append(&head, n);
- }
- }
- List(head);
- printf("\nAdd in begining: ");
- scanf("%d", &n);
- Append(&head, n);
- List(head);
- printf("\nDelete: ");
- Remove(&head);
- List(head);
- n=GetHead(head);
- printf("\nGetHead: %d\n",n);
- n=GetTail(head);
- printf("\nGetTail: %d\n",n);
- if (isEmpty(head)==0)
- printf("\nList empty\n");
- else
- printf("\nList not empty\n");
- free(head);
- }
- Node Create() {
- Node newNode = (Node * ) malloc(sizeof(Node));
- scanf("%d",&newNode->data);
- newNode->next = NULL;
- return (newNode);
- }
- void List(Node *A) {
- Node tmp;
- tmp = A;
- do {
- printf("%d ", tmp->data);
- tmp = tmp->next;
- } while (tmp != NULL);
- printf("\n");
- }
- void Append(Node *A, int d) {
- Node q = malloc(sizeof *q);
- assert(q != NULL);
- q->data = d;
- q->next = *A;
- *A = q;
- }
- void Remove(Node *A) {
- assert(*A != NULL);
- Node a = (*A)->next;
- free(*A);
- *A = a;
- }
- int GetHead(Node A) {
- assert(A != NULL);
- return A->data;
- }
- int GetTail(Node A) {
- assert(A!=NULL);
- while(A->next!=NULL)
- {
- A=A->next;
- if(A->next==NULL)
- {
- return A->data;
- }
- }
- }
- int isEmpty(Node A){
- if(A==NULL) return 0;
- else return 1;
- }
Add Comment
Please, Sign In to add comment