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);
- int Poisk(Node A,int c);
- Node Del(Node head);
- int Palindrom(Node *head,int c);
- 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): ");
- 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");
- printf("\nSearch, Enter element): ");
- scanf("%d", &n);
- n = Poisk(head,n);
- printf("Element: %d\n",n);
- if(Palindrom(&head,c)==0)
- printf("\nNot palindrom\n");
- else
- printf("\nPalindrom\n");
- printf("\nDel: ");
- head = Del(head);
- List(head);
- 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;
- }
- int Poisk(Node A,int c){
- int i = 0;
- while (i < c && A->next) {
- A = A->next;
- i++;
- }
- return A->data;
- }
- Node Del(Node head) {
- if (head == NULL) return;
- Node t = head;
- while (t != NULL) {
- Node p = t;
- while (p->next != NULL) {
- if (p->next->data == t->data) {
- p->next = p->next->next;
- } else{
- p = p->next;
- }
- }
- t = t->next;
- }
- return head;
- }
- int Palindrom(Node *head,int c) {
- if(c%2==0) {
- Node a = *head;
- const int z=c/2;
- int p=0;
- int m[z];
- for(int i=0;i<c/2;i++) {
- m[p]=a->data;
- a=a->next;
- p++;
- }
- int t=0;
- for(p=p-1;p>=0;p--) {
- if(a->data==m[p]) t++;
- else return 0;
- a=a->next;
- }
- if (t==c/2) return 1;
- else return 0;
- }
- else {
- Node a = *head;
- const int z=c/2;
- int p=0;
- int m[z];
- for(int i=0;i<c/2;i++) {
- m[p]=a->data;
- a=a->next;
- p++;
- if(i==c/2-1)a=a->next;
- }
- int t=0;
- for(p=p-1;p>=0;p--) {
- if(a->data==m[p]) t++;
- else return 0;
- a=a->next;
- }
- if (t==c/2) return 1;
- else return 0;
- }
- }
Add Comment
Please, Sign In to add comment